Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support lambda-based default values for params #510

Merged
merged 1 commit into from
Nov 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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