Skip to content

Commit

Permalink
Merge pull request #510 from myitcv/lambda_based_default_params
Browse files Browse the repository at this point in the history
Support lambda-based default values for params
  • Loading branch information
dblock committed Nov 20, 2013
2 parents c060202 + 5ba5d89 commit 5a7aa6f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
Excludes:
- vendor/**
- bin/**

LineLength:
Enabled: false
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Next Release
============

#### Features
* [#510](https://github.com/intridea/grape/pull/510): Support lambda-based default values for params - [@myitcv](https://github.com/myitcv).
* Your contribution here.

#### Fixes
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,17 @@ Optional parameters can have a default value.
```ruby
params do
optional :color, type: String, default: 'blue'
optional :random_number, type: Integer, default: -> { Random.rand(1..100) }
optional :non_random_number, type: Integer, default: Random.rand(1..100)
end
```

Parameters can be restricted to a specific set of values with the `:values` option.

Default values are eagerly evaluated. Above `:non_random_number` will evaluate to the same
number for each call to the endpoint of this `params` block. To have the default evaluate
at calltime use a lambda, like `:random_number` above.

```ruby
params do
requires :status, type: Symbol, values: [:not_started, :processing, :done]
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(attrs, options, required, scope)
end

def validate_param!(attr_name, params)
params[attr_name] = @default unless params.has_key?(attr_name)
params[attr_name] = @default.is_a?(Proc) ? @default.call : @default unless params.has_key?(attr_name)
end

def validate!(params)
Expand Down
20 changes: 20 additions & 0 deletions spec/grape/validations/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class API < Grape::API
get '/message' do
{ id: params[:id], type1: params[:type1], type2: params[:type2] }
end

params do
optional :random, default: -> { Random.rand }
optional :not_random, default: Random.rand
end
get '/numbers' do
{ random_number: params[:random], non_random_number: params[:non_random_number] }
end
end
end
end
Expand Down Expand Up @@ -63,4 +71,16 @@ def app
last_response.status.should == 200
last_response.body.should == { id: '1', type1: 'default-type1', type2: 'default-type2' }.to_json
end

it 'sets lambda based defaults at the time of call' do
get("/numbers")
last_response.status.should == 200
before = JSON.parse(last_response.body)
get("/numbers")
last_response.status.should == 200
after = JSON.parse(last_response.body)

before['non_random_number'].should == after['non_random_number']
before['random_number'].should_not == after['random_number']
end
end

0 comments on commit 5a7aa6f

Please sign in to comment.