Skip to content

Commit dcba3a2

Browse files
author
James McCarthy
committed
Update spec and docs for lambda and proc syntax.
1 parent 4c991ea commit dcba3a2

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/grape_entity/entity.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,18 @@ def self.inherited(subclass)
130130
# If a proc is presented it is evaluated in the context of the entity so object
131131
# and the entity methods are available to it.
132132
#
133-
# @example as: a proc
133+
# @example as: a proc or lambda
134134
#
135-
# object = OpenStruct(awesomness: 'awesome_key', awesome: 'not-my-key' )
135+
# object = OpenStruct(awesomness: 'awesome_key', awesome: 'not-my-key', other: 'other-key' )
136136
#
137137
# 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 }
139140
# end
140141
#
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.
142145
#
143146
# @option options :if When passed a Hash, the attribute will only be exposed if the
144147
# runtime options match all the conditions passed in. When passed a lambda, the

spec/grape_entity/exposure_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535
end
3636

3737
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 }
3944
expect(subject.key(entity)).to eq(model.name.reverse)
4045
end
4146
end

0 commit comments

Comments
 (0)