Skip to content

Commit 4032ea8

Browse files
committed
Merge pull request #1287 from totothink/master
clean DSL module
2 parents 84e5ec6 + 6a49d6c commit 4032ea8

File tree

8 files changed

+228
-186
lines changed

8 files changed

+228
-186
lines changed

lib/grape.rb

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ module DSL
147147
autoload :RequestResponse
148148
autoload :Routing
149149
autoload :Validations
150+
autoload :Logger
151+
autoload :Desc
150152
end
151153
end
152154

lib/grape/dsl/configuration.rb

+2-107
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,9 @@ module Configuration
66
extend ActiveSupport::Concern
77

88
module ClassMethods
9-
attr_writer :logger
10-
119
include Grape::DSL::Settings
12-
13-
# Set or retrive the configured logger. If none was configured, this
14-
# method will create a new one, logging to stdout.
15-
# @param logger [Object] the new logger to use
16-
def logger(logger = nil)
17-
if logger
18-
global_setting(:logger, logger)
19-
else
20-
global_setting(:logger) || global_setting(:logger, Logger.new($stdout))
21-
end
22-
end
23-
24-
# Add a description to the next namespace or function.
25-
# @param description [String] descriptive string for this endpoint
26-
# or namespace
27-
# @param options [Hash] other properties you can set to describe the
28-
# endpoint or namespace. Optional.
29-
# @option options :detail [String] additional detail about this endpoint
30-
# @option options :params [Hash] param types and info. normally, you set
31-
# these via the `params` dsl method.
32-
# @option options :entity [Grape::Entity] the entity returned upon a
33-
# successful call to this action
34-
# @option options :http_codes [Array[Array]] possible HTTP codes this
35-
# endpoint may return, with their meanings, in a 2d array
36-
# @option options :named [String] a specific name to help find this route
37-
# @option options :headers [Hash] HTTP headers this method can accept
38-
# @yield a block yielding an instance context with methods mapping to
39-
# each of the above, except that :entity is also aliased as #success
40-
# and :http_codes is aliased as #failure.
41-
#
42-
# @example
43-
#
44-
# desc 'create a user'
45-
# post '/users' do
46-
# # ...
47-
# end
48-
#
49-
# desc 'find a user' do
50-
# detail 'locates the user from the given user ID'
51-
# failure [ [404, 'Couldn\'t find the given user' ] ]
52-
# success User::Entity
53-
# end
54-
# get '/user/:id' do
55-
# # ...
56-
# end
57-
#
58-
def desc(description, options = {}, &config_block)
59-
if block_given?
60-
config_class = Grape::DSL::Configuration.desc_container
61-
62-
config_class.configure do
63-
description description
64-
end
65-
66-
config_class.configure(&config_block)
67-
unless options.empty?
68-
warn '[DEPRECATION] Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.'
69-
end
70-
options = config_class.settings
71-
else
72-
options = options.merge(description: description)
73-
end
74-
75-
namespace_setting :description, options
76-
route_setting :description, options
77-
end
78-
79-
def description_field(field, value = nil)
80-
if value
81-
description = route_setting(:description)
82-
description ||= route_setting(:description, {})
83-
description[field] = value
84-
else
85-
description = route_setting(:description)
86-
description[field] if description
87-
end
88-
end
89-
90-
def unset_description_field(field)
91-
description = route_setting(:description)
92-
description.delete(field) if description
93-
end
10+
include Grape::DSL::Logger
11+
include Grape::DSL::Desc
9412
end
9513

9614
module_function
@@ -100,29 +18,6 @@ def stacked_hash_to_hash(settings)
10018
return if settings.blank?
10119
settings.each_with_object({}) { |value, result| result.deep_merge!(value) }
10220
end
103-
104-
# Returns an object which configures itself via an instance-context DSL.
105-
def desc_container
106-
Module.new do
107-
include Grape::Util::StrictHashConfiguration.module(
108-
:description,
109-
:detail,
110-
:params,
111-
:entity,
112-
:http_codes,
113-
:named,
114-
:headers
115-
)
116-
117-
def config_context.success(*args)
118-
entity(*args)
119-
end
120-
121-
def config_context.failure(*args)
122-
http_codes(*args)
123-
end
124-
end
125-
end
12621
end
12722
end
12823
end

lib/grape/dsl/desc.rb

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
module Grape
2+
module DSL
3+
module Desc
4+
include Grape::DSL::Settings
5+
6+
# Add a description to the next namespace or function.
7+
# @param description [String] descriptive string for this endpoint
8+
# or namespace
9+
# @param options [Hash] other properties you can set to describe the
10+
# endpoint or namespace. Optional.
11+
# @option options :detail [String] additional detail about this endpoint
12+
# @option options :params [Hash] param types and info. normally, you set
13+
# these via the `params` dsl method.
14+
# @option options :entity [Grape::Entity] the entity returned upon a
15+
# successful call to this action
16+
# @option options :http_codes [Array[Array]] possible HTTP codes this
17+
# endpoint may return, with their meanings, in a 2d array
18+
# @option options :named [String] a specific name to help find this route
19+
# @option options :headers [Hash] HTTP headers this method can accept
20+
# @yield a block yielding an instance context with methods mapping to
21+
# each of the above, except that :entity is also aliased as #success
22+
# and :http_codes is aliased as #failure.
23+
#
24+
# @example
25+
#
26+
# desc 'create a user'
27+
# post '/users' do
28+
# # ...
29+
# end
30+
#
31+
# desc 'find a user' do
32+
# detail 'locates the user from the given user ID'
33+
# failure [ [404, 'Couldn\'t find the given user' ] ]
34+
# success User::Entity
35+
# end
36+
# get '/user/:id' do
37+
# # ...
38+
# end
39+
#
40+
def desc(description, options = {}, &config_block)
41+
if block_given?
42+
config_class = desc_container
43+
44+
config_class.configure do
45+
description description
46+
end
47+
48+
config_class.configure(&config_block)
49+
unless options.empty?
50+
warn '[DEPRECATION] Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.'
51+
end
52+
options = config_class.settings
53+
else
54+
options = options.merge(description: description)
55+
end
56+
57+
namespace_setting :description, options
58+
route_setting :description, options
59+
end
60+
61+
def description_field(field, value = nil)
62+
if value
63+
description = route_setting(:description)
64+
description ||= route_setting(:description, {})
65+
description[field] = value
66+
else
67+
description = route_setting(:description)
68+
description[field] if description
69+
end
70+
end
71+
72+
def unset_description_field(field)
73+
description = route_setting(:description)
74+
description.delete(field) if description
75+
end
76+
77+
# Returns an object which configures itself via an instance-context DSL.
78+
def desc_container
79+
Module.new do
80+
include Grape::Util::StrictHashConfiguration.module(
81+
:description,
82+
:detail,
83+
:params,
84+
:entity,
85+
:http_codes,
86+
:named,
87+
:headers
88+
)
89+
90+
def config_context.success(*args)
91+
entity(*args)
92+
end
93+
94+
def config_context.failure(*args)
95+
http_codes(*args)
96+
end
97+
end
98+
end
99+
end
100+
end
101+
end

lib/grape/dsl/logger.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Grape
2+
module DSL
3+
module Logger
4+
include Grape::DSL::Settings
5+
6+
attr_writer :logger
7+
8+
# Set or retrive the configured logger. If none was configured, this
9+
# method will create a new one, logging to stdout.
10+
# @param logger [Object] the new logger to use
11+
def logger(logger = nil)
12+
if logger
13+
global_setting(:logger, logger)
14+
else
15+
global_setting(:logger) || global_setting(:logger, ::Logger.new($stdout))
16+
end
17+
end
18+
end
19+
end
20+
end

lib/grape/dsl/routing.rb

-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ def version(*args, &block)
4646
namespace_inheritable(:version, args)
4747
namespace_inheritable(:version_options, options)
4848
end
49-
50-
# reset_validations!
5149
end
5250

5351
@versions.last unless @versions.nil?
@@ -91,8 +89,6 @@ def mount(mounts)
9189

9290
in_setting = app.top_level_setting
9391

94-
# app.regenerate_endpoints(in_setting)
95-
9692
app.change!
9793
change!
9894
end

spec/grape/dsl/configuration_spec.rb

-75
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,6 @@ class Dummy
99
end
1010
describe Configuration do
1111
subject { Class.new(ConfigurationSpec::Dummy) }
12-
let(:logger) { double(:logger) }
13-
14-
describe '.logger' do
15-
it 'sets a logger' do
16-
subject.logger logger
17-
expect(subject.logger).to eq logger
18-
end
19-
20-
it 'returns a logger' do
21-
expect(subject.logger logger).to eq logger
22-
end
23-
end
24-
25-
describe '.desc' do
26-
it 'sets a description' do
27-
desc_text = 'The description'
28-
options = { message: 'none' }
29-
subject.desc desc_text, options
30-
expect(subject.namespace_setting(:description)).to eq(options.merge(description: desc_text))
31-
expect(subject.route_setting(:description)).to eq(options.merge(description: desc_text))
32-
end
33-
34-
it 'can be set with a block' do
35-
expected_options = {
36-
description: 'The description',
37-
detail: 'more details',
38-
params: { first: :param },
39-
entity: Object,
40-
http_codes: [[401, 'Unauthorized', 'Entities::Error']],
41-
named: 'My named route',
42-
headers: [XAuthToken: {
43-
description: 'Valdates your identity',
44-
required: true
45-
},
46-
XOptionalHeader: {
47-
description: 'Not really needed',
48-
required: false
49-
}
50-
]
51-
}
52-
53-
subject.desc 'The description' do
54-
detail 'more details'
55-
params(first: :param)
56-
success Object
57-
failure [[401, 'Unauthorized', 'Entities::Error']]
58-
named 'My named route'
59-
headers [XAuthToken: {
60-
description: 'Valdates your identity',
61-
required: true
62-
},
63-
XOptionalHeader: {
64-
description: 'Not really needed',
65-
required: false
66-
}
67-
]
68-
end
69-
70-
expect(subject.namespace_setting(:description)).to eq(expected_options)
71-
expect(subject.route_setting(:description)).to eq(expected_options)
72-
end
73-
74-
it 'can be set with options and a block' do
75-
expect(subject).to receive(:warn).with('[DEPRECATION] Passing a options hash and a block to `desc` is deprecated. Move all hash options to block.')
76-
77-
desc_text = 'The description'
78-
detail_text = 'more details'
79-
options = { message: 'none' }
80-
subject.desc desc_text, options do
81-
detail detail_text
82-
end
83-
expect(subject.namespace_setting(:description)).to eq(description: desc_text, detail: detail_text)
84-
expect(subject.route_setting(:description)).to eq(description: desc_text, detail: detail_text)
85-
end
86-
end
8712
end
8813
end
8914
end

0 commit comments

Comments
 (0)