Skip to content

Commit 62b8b76

Browse files
committed
Extended ParamBulder to be included in an entire API or namespace.
1 parent d48333a commit 62b8b76

18 files changed

+387
-212
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#### Features
44

5-
* [#1594](https://github.com/ruby-grape/grape/pull/1594): Replace `Hashie::Mash` parameters with `ActiveSupport::HashWithIndifferentAccess` - [@james2m](https://github.com/james2m), [@dblock](https://github.com/dblock).
5+
* [#1610](https://github.com/ruby-grape/grape/pull/1610): Replace `Hashie::Mash` parameters with `ActiveSupport::HashWithIndifferentAccess`, removed dependency on `hashie` - [@james2m](https://github.com/james2m), [@dblock](https://github.com/dblock).
66
* [#1555](https://github.com/ruby-grape/grape/pull/1555): Added code coverage w/Coveralls - [@dblock](https://github.com/dblock).
77
* [#1568](https://github.com/ruby-grape/grape/pull/1568): Add `proc` option to `values` validator to allow custom checks - [@jlfaber](https://github.com/jlfaber).
88
* [#1575](https://github.com/ruby-grape/grape/pull/1575): Include nil values for missing nested params in declared - [@thogg4](https://github.com/thogg4).

README.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [Param](#param)
3030
- [Describing Methods](#describing-methods)
3131
- [Parameters](#parameters)
32+
- [Params Class](#params-class)
3233
- [Declared](#declared)
3334
- [Include Missing](#include-missing)
3435
- [Parameter Validation and Coercion](#parameter-validation-and-coercion)
@@ -501,9 +502,19 @@ Route string parameters will have precedence.
501502

502503
By default parameters are available as `ActiveSupport::HashWithIndifferentAccess`. This can be changed to, for example, Ruby `Hash` or `Hashie::Mash` for the entire API.
503504

504-
[TODO]
505+
```ruby
506+
class API < Grape::API
507+
include Grape::Extensions::Hashie::Mash::ParamBuilder
508+
509+
params do
510+
optional :color, type: String
511+
end
512+
get do
513+
params.color # instead of params[:color]
514+
end
515+
```
505516

506-
The class can be overridden on individual parameter blocks using `build_with` as follows.
517+
The class can also be overridden on individual parameter blocks using `build_with` as follows.
507518

508519
```ruby
509520
params do
@@ -514,6 +525,8 @@ end
514525

515526
In the example above, `params["color"]` will return `nil` since `params` is a plain `Hash`.
516527

528+
Available parameter builders are `Grape::Extensions::Hash::ParamBuilder`, `Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder` and `Grape::Extensions::Hashie::Mash::ParamBuilder`.
529+
517530
### Declared
518531

519532
Grape allows you to access only the parameters that have been declared by your `params` block. It filters out the params that have been passed, but are not allowed. Consider the following API endpoint:
@@ -526,7 +539,7 @@ post 'users/signup' do
526539
end
527540
````
528541

529-
If we do not specify any params, `declared` will return an empty `ActiveSupport::HashWithIndifferentAccess` hash.
542+
If you do not specify any parameters, `declared` will return an empty hash.
530543

531544
**Request**
532545

@@ -543,8 +556,8 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
543556

544557
````
545558

546-
Once we add parameters requirements, grape will start returning only the declared params[:
547-
]
559+
Once we add parameters requirements, grape will start returning only the declared parameters.
560+
548561
````ruby
549562
format :json
550563

@@ -579,7 +592,7 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
579592
}
580593
````
581594

582-
The returned hash is a `ActiveSupport::HashWithIndifferentAccess` hash.
595+
The returned hash is an `ActiveSupport::HashWithIndifferentAccess`.
583596

584597
The `#declared` method is not available to `before` filters, as those are evaluated prior to parameter coercion.
585598

UPGRADING.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@ Upgrading Grape
55

66
#### Changes in Parameter Class
77

8-
The default class for `params` has changed from `Hashie::Mash` to `ActiveSupport::HashWithIndifferentAccess` and the `hashie` dependency has been removed. To restore the behavior of prior versions, add `hashie` to your `Gemfile` and use `TODO`.
8+
The default class for `params` has changed from `Hashie::Mash` to `ActiveSupport::HashWithIndifferentAccess` and the `hashie` dependency has been removed. This means that by default you can no longer access parameters by method name.
99

10-
[TODO]
10+
```
11+
class API < Grape::API
12+
params do
13+
optional :color, type: String
14+
end
15+
get do
16+
params[:color] # use params[:color] instead of params.color
17+
end
18+
end
19+
```
20+
21+
To restore the behavior of prior versions, add `hashie` to your `Gemfile` and `include Grape::Extensions::Hashie::Mash::ParamBuilder` in your API.
22+
23+
```
24+
class API < Grape::API
25+
include Grape::Extensions::Hashie::Mash::ParamBuilder
26+
27+
params do
28+
optional :color, type: String
29+
end
30+
get do
31+
# params.color works
32+
end
33+
end
34+
```
1135

1236
This behavior can also be overridden on individual parameter blocks using `build_with`.
1337

@@ -26,7 +50,7 @@ def request
2650
end
2751
```
2852

29-
See [#1594](https://github.com/ruby-grape/grape/pull/1594) for more information.
53+
See [#1610](https://github.com/ruby-grape/grape/pull/1610) for more information.
3054

3155
### Upgrading to >= 0.19.1
3256

lib/grape.rb

+22-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
I18n.load_path << File.expand_path('../grape/locale/en.yml', __FILE__)
2727

2828
module Grape
29-
extend ActiveSupport::Autoload
29+
extend ::ActiveSupport::Autoload
3030

3131
eager_autoload do
3232
autoload :API
@@ -47,14 +47,14 @@ module Grape
4747
end
4848

4949
module Http
50-
extend ActiveSupport::Autoload
50+
extend ::ActiveSupport::Autoload
5151
eager_autoload do
5252
autoload :Headers
5353
end
5454
end
5555

5656
module Exceptions
57-
extend ActiveSupport::Autoload
57+
extend ::ActiveSupport::Autoload
5858
autoload :Base
5959
autoload :Validation
6060
autoload :ValidationArrayErrors
@@ -78,22 +78,27 @@ module Exceptions
7878
end
7979

8080
module Extensions
81-
extend ActiveSupport::Autoload
81+
extend ::ActiveSupport::Autoload
8282

8383
autoload :DeepMergeableHash
8484
autoload :DeepSymbolizeHash
8585
autoload :Hash
86-
autoload :HashWithIndifferentAccess
86+
87+
module ActiveSupport
88+
extend ::ActiveSupport::Autoload
89+
90+
autoload :HashWithIndifferentAccess
91+
end
8792

8893
module Hashie
89-
extend ActiveSupport::Autoload
94+
extend ::ActiveSupport::Autoload
9095

9196
autoload :Mash
9297
end
9398
end
9499

95100
module Middleware
96-
extend ActiveSupport::Autoload
101+
extend ::ActiveSupport::Autoload
97102
autoload :Base
98103
autoload :Versioner
99104
autoload :Formatter
@@ -102,15 +107,15 @@ module Middleware
102107
autoload :Stack
103108

104109
module Auth
105-
extend ActiveSupport::Autoload
110+
extend ::ActiveSupport::Autoload
106111
autoload :Base
107112
autoload :DSL
108113
autoload :StrategyInfo
109114
autoload :Strategies
110115
end
111116

112117
module Versioner
113-
extend ActiveSupport::Autoload
118+
extend ::ActiveSupport::Autoload
114119
autoload :Path
115120
autoload :Header
116121
autoload :Param
@@ -119,7 +124,7 @@ module Versioner
119124
end
120125

121126
module Util
122-
extend ActiveSupport::Autoload
127+
extend ::ActiveSupport::Autoload
123128
autoload :InheritableValues
124129
autoload :StackableValues
125130
autoload :ReverseStackableValues
@@ -129,29 +134,29 @@ module Util
129134
end
130135

131136
module ErrorFormatter
132-
extend ActiveSupport::Autoload
137+
extend ::ActiveSupport::Autoload
133138
autoload :Base
134139
autoload :Json
135140
autoload :Txt
136141
autoload :Xml
137142
end
138143

139144
module Formatter
140-
extend ActiveSupport::Autoload
145+
extend ::ActiveSupport::Autoload
141146
autoload :Json
142147
autoload :SerializableHash
143148
autoload :Txt
144149
autoload :Xml
145150
end
146151

147152
module Parser
148-
extend ActiveSupport::Autoload
153+
extend ::ActiveSupport::Autoload
149154
autoload :Json
150155
autoload :Xml
151156
end
152157

153158
module DSL
154-
extend ActiveSupport::Autoload
159+
extend ::ActiveSupport::Autoload
155160
eager_autoload do
156161
autoload :API
157162
autoload :Callbacks
@@ -170,17 +175,17 @@ module DSL
170175
end
171176

172177
class API
173-
extend ActiveSupport::Autoload
178+
extend ::ActiveSupport::Autoload
174179
autoload :Helpers
175180
end
176181

177182
module Presenters
178-
extend ActiveSupport::Autoload
183+
extend ::ActiveSupport::Autoload
179184
autoload :Presenter
180185
end
181186

182187
module ServeFile
183-
extend ActiveSupport::Autoload
188+
extend ::ActiveSupport::Autoload
184189
autoload :FileResponse
185190
autoload :FileBody
186191
autoload :SendfileResponse

lib/grape/dsl/parameters.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ module Parameters
1111
# Set the module used to build the request.params.
1212
#
1313
# @param build_with the ParamBuilder module to use when building request.params
14-
# Available builders are;
15-
# * Grape::Extensions::HashWithIndifferentAccess::ParamBuilder (default)
14+
# Available builders are:
15+
#
16+
# * Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder (default)
1617
# * Grape::Extensions::Hash::ParamBuilder
1718
# * Grape::Extensions::Hashie::Mash::ParamBuilder
1819
#
@@ -30,7 +31,7 @@ module Parameters
3031
# end
3132
# end
3233
def build_with(build_with = nil)
33-
@api.namespace_inheritable(:build_with, build_with)
34+
@api.namespace_inheritable(:build_params_with, build_with)
3435
end
3536

3637
# Include reusable params rules among current.

lib/grape/endpoint.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def equals?(e)
237237
def run
238238
ActiveSupport::Notifications.instrument('endpoint_run.grape', endpoint: self, env: env) do
239239
@header = {}
240-
@request = Grape::Request.new(env, build_params_with: namespace_inheritable(:build_with))
240+
@request = Grape::Request.new(env, build_params_with: namespace_inheritable(:build_params_with))
241241
@params = @request.params
242242
@headers = @request.headers
243243

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Grape
2+
module Extensions
3+
module ActiveSupport
4+
module HashWithIndifferentAccess
5+
extend ::ActiveSupport::Concern
6+
7+
included do
8+
namespace_inheritable(:build_params_with, Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder)
9+
end
10+
11+
def params_builder
12+
Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
13+
end
14+
15+
module ParamBuilder
16+
def build_params
17+
params = ::ActiveSupport::HashWithIndifferentAccess[rack_params]
18+
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS]
19+
params
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end

lib/grape/extensions/hash.rb

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ module Grape
22
module Extensions
33
module Hash
44
module ParamBuilder
5+
extend ::ActiveSupport::Concern
6+
7+
included do
8+
namespace_inheritable(:build_params_with, Grape::Extensions::Hash::ParamBuilder)
9+
end
10+
511
def build_params
612
params = Grape::Extensions::DeepMergeableHash[rack_params]
713
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS]

lib/grape/extensions/hash_with_indifferent_access.rb

-17
This file was deleted.

lib/grape/extensions/hashie/mash.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ module Grape
22
module Extensions
33
module Hashie
44
module Mash
5-
def params_builder
6-
Grape::Extensions::Hashie::Mash::ParamBuilder
7-
end
8-
95
module ParamBuilder
6+
extend ::ActiveSupport::Concern
7+
8+
included do
9+
namespace_inheritable(:build_params_with, Grape::Extensions::Hashie::Mash::ParamBuilder)
10+
end
11+
12+
def params_builder
13+
Grape::Extensions::Hashie::Mash::ParamBuilder
14+
end
15+
1016
def build_params
1117
params = ::Hashie::Mash.new(rack_params)
1218
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS]

lib/grape/request.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Request < Rack::Request
55
alias rack_params params
66

77
def initialize(env, options = {})
8-
extend options[:build_params_with] || Grape::Extensions::HashWithIndifferentAccess::ParamBuilder
8+
extend options[:build_params_with] || Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder
99
super(env)
1010
end
1111

lib/grape/validations/types/custom_type_coercer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def infer_type_check(type)
147147
# Enforce symbolized keys for complex types
148148
# by wrapping the coercion method such that
149149
# any Hash objects in the immediate heirarchy
150-
# have their keys recursively symbolized
150+
# have their keys recursively symbolized.
151151
# This helps common libs such as JSON to work easily.
152152
#
153153
# @param type see #new

0 commit comments

Comments
 (0)