Skip to content

Commit 5d1a95d

Browse files
committed
explicit kwargs for namespace and route_param
1 parent 036ee8e commit 5d1a95d

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* [#2649](https://github.com/ruby-grape/grape/pull/2644): Drop support Ruby 3.0 and ActiveSupport 7.0 - [@ericproulx](https://github.com/ericproulx).
1616
* [#2648](https://github.com/ruby-grape/grape/pull/2648): Remove deprecated ParamsBuilders extensions - [@ericproulx](https://github.com/ericproulx).
1717
* [#2645](https://github.com/ruby-grape/grape/pull/2645): Endpoints are compiled when API is compiled - [@ericproulx](https://github.com/ericproulx).
18-
18+
* [#2647](https://github.com/ruby-grape/grape/pull/2647): Explicit kwargs for `namespace` and `route_param` - [@ericproulx](https://github.com/ericproulx).
1919
* Your contribution here.
2020

2121
#### Fixes

UPGRADING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ Upgrading Grape
33

44
### Upgrading to >= 3.1
55

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+
610
#### ParamsBuilder Grape::Extensions
711

812
Deprecated [ParamsBuilder's extensions](https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#params-builder) have been removed.
913

10-
### Upgrading to >= 3.1.0
11-
1214
#### Enhanced API compile!
1315

1416
Endpoints are now "compiled" instead of lazy loaded. Historically, when calling `YourAPI.compile!` in `config.ru` (or just receiving the first API call), only routing was compiled see [Grape::Router#compile!](https://github.com/ruby-grape/grape/blob/bf90e95c3b17c415c944363b1c07eb9727089ee7/lib/grape/router.rb#L41-L54) and endpoints were lazy loaded. Now, it's part of the API compilation. See [#2645](https://github.com/ruby-grape/grape/pull/2645) for more information.

lib/grape/dsl/routing.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ def route(methods, paths = ['/'], route_options = {}, &)
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,16 +223,12 @@ 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, &)
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, &)
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

237233
namespace(":#{param}", options, &)
238234
end

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)