Skip to content

Commit

Permalink
Remove deprecation msg + small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Oct 1, 2024
1 parent 7f0595c commit 13528b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
51 changes: 27 additions & 24 deletions lib/grape/dsl/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,23 @@ module Desc
# # ...
# end
#
def desc(description, options = {}, &config_block)
if config_block
endpoint_configuration = if defined?(configuration)
# When the instance is mounted - the configuration is executed on mount time
if configuration.respond_to?(:evaluate)
configuration.evaluate
# Within `given` or `mounted blocks` the configuration is already evaluated
elsif configuration.is_a?(Hash)
configuration
end
end
endpoint_configuration ||= {}
config_class = desc_container(endpoint_configuration)
def desc(description, options = nil, &config_block)
opts =
if config_block
desc_container(endpoint_configuration).then do |config_class|
config_class.configure do
description(description)
end

config_class.configure do
description description
config_class.configure(&config_block)
config_class.settings
end
else
options&.merge(description: description) || { description: description }
end

config_class.configure(&config_block)
Grape.deprecator.warn('Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.') if options.any?
options = config_class.settings
else
options = options.merge(description: description)
end

namespace_setting :description, options
route_setting :description, options
namespace_setting :description, opts
route_setting :description, opts
end

# Returns an object which configures itself via an instance-context DSL.
Expand All @@ -116,6 +106,19 @@ def config_context.failure(*args)
end
end
end

private

def endpoint_configuration
return {} unless defined?(configuration)

if configuration.respond_to?(:evaluate)
configuration.evaluate
# Within `given` or `mounted blocks` the configuration is already evaluated
elsif configuration.is_a?(Hash)
configuration
end
end
end
end
end
13 changes: 0 additions & 13 deletions spec/grape/dsl/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,5 @@
expect(subject.namespace_setting(:description)).to eq(expected_options)
expect(subject.route_setting(:description)).to eq(expected_options)
end

it 'can be set with options and a block' do
expect(Grape.deprecator).to receive(:warn).with('Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.')

desc_text = 'The description'
detail_text = 'more details'
options = { message: 'none' }
subject.desc desc_text, options do
detail detail_text
end
expect(subject.namespace_setting(:description)).to eq(description: desc_text, detail: detail_text)
expect(subject.route_setting(:description)).to eq(description: desc_text, detail: detail_text)
end
end
end

0 comments on commit 13528b4

Please sign in to comment.