Skip to content

Commit 732f2e5

Browse files
author
James McCarthy
committed
Update spec for lambda and proc syntax.
1 parent 429b0ec commit 732f2e5

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/grape_entity/entity.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,18 @@ def self.inherited(subclass)
128128
# If a proc is presented it is evaluated in the context of the entity so object
129129
# and the entity methods are available to it.
130130
#
131-
# @example as: a proc
131+
# @example as: a proc or lambda
132132
#
133-
# object = OpenStruct(awesomness: 'awesome_key', awesome: 'not-my-key' )
133+
# object = OpenStruct(awesomness: 'awesome_key', awesome: 'not-my-key', other: 'other-key' )
134134
#
135135
# 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 }
137138
# end
138139
#
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.
140143
#
141144
# @option options :if When passed a Hash, the attribute will only be exposed if the
142145
# runtime options match all the conditions passed in. When passed a lambda, the

spec/grape_entity/exposure_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
end
3434

3535
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 }
3742
expect(subject.key(entity)).to eq(model.name.reverse)
3843
end
3944
end

0 commit comments

Comments
 (0)