Skip to content

Commit 031fbca

Browse files
committed
explicit kwargs for namespace and route_param
1 parent bf90e95 commit 031fbca

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [#2643](https://github.com/ruby-grape/grape/pull/2638): Remove `try` method in codebase - [@ericproulx](https://github.com/ericproulx).
1313
* [#2646](https://github.com/ruby-grape/grape/pull/2646): Call `valid_encoding?` before scrub - [@ericproulx](https://github.com/ericproulx).
1414
* [#2644](https://github.com/ruby-grape/grape/pull/2644): Clean useless/not valuable dependencies - [@ericproulx](https://github.com/ericproulx).
15+
* [#2648](https://github.com/ruby-grape/grape/pull/2648): Explicit kwargs for `namespace` and `route_param` - [@ericproulx](https://github.com/ericproulx).
1516
* Your contribution here.
1617

1718
#### Fixes

UPGRADING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 3.1
5+
6+
#### Explicit kwargs for `namespace` and `route_param`
7+
8+
The `API#namespace` and `route_param` methods are now defined with `**options` instead of `options = {}`. In addtion, `requirements` in explicitly defined so it's not in `options` anymore. You can still call `requirements` like before but `options[:requirements]` will be empty. For `route_param`, `type` is also an explicit parameter so it's not in `options` anymore. See [#2647](https://github.com/ruby-grape/grape/pull/2647) for more information.
9+
410
### Upgrading to >= 3.0.0
511

612
#### Ruby 3+ Argument Delegation Modernization

lib/grape/dsl/routing.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ def route(methods, paths = ['/'], route_options = {}, &block)
198198
# # defines the endpoint: GET /foo/bar
199199
# end
200200
# end
201-
def namespace(space = nil, options = {}, &block)
201+
def namespace(space = nil, requirements: nil, **options, &block)
202202
return Namespace.joined_space_path(inheritable_setting.namespace_stackable[:namespace]) unless space || block
203203

204204
within_namespace do
205205
nest(block) do
206-
inheritable_setting.namespace_stackable[:namespace] = Grape::Namespace.new(space, options) if space
206+
inheritable_setting.namespace_stackable[:namespace] = Grape::Namespace.new(space, requirements: requirements, **options) if space
207207
end
208208
end
209209
end
@@ -223,18 +223,14 @@ def routes
223223
#
224224
# @param param [Symbol] The name of the parameter you wish to declare.
225225
# @option options [Regexp] You may supply a regular expression that the declared parameter must meet.
226-
def route_param(param, **options, &block)
227-
options = options.dup
228-
229-
options[:requirements] = {
230-
param.to_sym => options[:requirements]
231-
} if options[:requirements].is_a?(Regexp)
226+
def route_param(param, requirements: nil, type: nil, **options, &block)
227+
requirements = { param.to_sym => requirements } if requirements.is_a?(Regexp)
232228

233229
Grape::Validations::ParamsScope.new(api: self) do
234-
requires param, type: options[:type]
235-
end if options.key?(:type)
230+
requires param, type: type
231+
end if type
236232

237-
namespace(":#{param}", options, &block)
233+
namespace(":#{param}", requirements: requirements, **options, &block)
238234
end
239235

240236
# @return array of defined versions

lib/grape/namespace.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ module Grape
55
# logical grouping of endpoints as well as sharing common configuration.
66
# May also be referred to as group, segment, or resource.
77
class Namespace
8-
attr_reader :space, :options
8+
attr_reader :space, :requirements, :options
99

1010
# @param space [String] the name of this namespace
1111
# @param options [Hash] options hash
1212
# @option options :requirements [Hash] param-regex pairs, all of which must
1313
# be met by a request's params for all endpoints in this namespace, or
1414
# validation will fail and return a 422.
15-
def initialize(space, options)
15+
def initialize(space, requirements: nil, **options)
1616
@space = space.to_s
17+
@requirements = requirements
1718
@options = options
1819
end
1920

20-
# Retrieves the requirements from the options hash, if given.
21-
# @return [Hash]
22-
def requirements
23-
options[:requirements] || {}
24-
end
25-
2621
# (see ::joined_space_path)
2722
def self.joined_space(settings)
2823
settings&.map(&:space)
@@ -31,12 +26,13 @@ def self.joined_space(settings)
3126
def eql?(other)
3227
other.class == self.class &&
3328
other.space == space &&
29+
other.requirements == requirements &&
3430
other.options == options
3531
end
3632
alias == eql?
3733

3834
def hash
39-
[self.class, space, options].hash
35+
[self.class, space, requirements, options].hash
4036
end
4137

4238
# Join the namespaces from a list of settings to create a path prefix.

spec/grape/dsl/routing_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class << self
280280
let(:regex) { /(.*)/ }
281281

282282
it 'calls #namespace with given params' do
283-
expect(subject).to receive(:namespace).with(':foo', {}).and_yield
283+
expect(subject).to receive(:namespace).with(':foo', requirements: nil).and_yield
284284
subject.route_param('foo', &proc {})
285285
end
286286

0 commit comments

Comments
 (0)