File tree Expand file tree Collapse file tree 2 files changed +13
-5
lines changed Expand file tree Collapse file tree 2 files changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -128,15 +128,18 @@ def self.inherited(subclass)
128
128
# If a proc is presented it is evaluated in the context of the entity so object
129
129
# and the entity methods are available to it.
130
130
#
131
- # @example as: a proc
131
+ # @example as: a proc or lambda
132
132
#
133
- # object = OpenStruct(awesomness: 'awesome_key', awesome: 'not-my-key' )
133
+ # object = OpenStruct(awesomness: 'awesome_key', awesome: 'not-my-key', other: 'other-key' )
134
134
#
135
135
# class MyEntity < Grape::Entity
136
- # expose :awesome, as: -> { object.awesomness }
136
+ # expose :awesome, as: Proc.new { object.awesomeness }
137
+ # expose :awesomeness, as: ->(object, opts) { object.other }
137
138
# end
138
139
#
139
- # => { 'awesome_key': 'not-my-key' }
140
+ # => { 'awesome_key': 'not-my-key', 'other-key': 'awesome_key' }
141
+ #
142
+ # Note the parameters passed in via the lambda syntax.
140
143
#
141
144
# @option options :if When passed a Hash, the attribute will only be exposed if the
142
145
# runtime options match all the conditions passed in. When passed a lambda, the
Original file line number Diff line number Diff line change 33
33
end
34
34
35
35
it 'returns the result if :as is a proc' do
36
- fresh_class . expose :name , as : -> { object . name . reverse }
36
+ fresh_class . expose :name , as : Proc . new { object . name . reverse }
37
+ expect ( subject . key ( entity ) ) . to eq ( model . name . reverse )
38
+ end
39
+
40
+ it 'returns the result if :as is a lambda' do
41
+ fresh_class . expose :name , as : -> ( obj , opts ) { obj . name . reverse }
37
42
expect ( subject . key ( entity ) ) . to eq ( model . name . reverse )
38
43
end
39
44
end
You can’t perform that action at this time.
0 commit comments