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