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

Call procs in context of entity instance #43

Merged
merged 5 commits into from
Jan 7, 2014
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ tmp
.rbx

## PROJECT::SPECIFIC
.project
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Next Release
============
* Ruby 1.8.x is no longer supported - [@dblock](https://github.com/dblock).
* [#36](https://github.com/intridea/grape-entity/pull/35): Enforcing Ruby style guidelines via Rubocop - [@dblock](https://github.com/dblock).
* [#36](https://github.com/intridea/grape-entity/pull/36): Enforcing Ruby style guidelines via Rubocop - [@dblock](https://github.com/dblock).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem :-)

* [#7](https://github.com/intridea/grape-entity/issues/7): Added `serializable` option to `represent` - [@mbleigh](https://github.com/mbleigh).
* [#18](https://github.com/intridea/grape-entity/pull/18): Added `safe` option to `expose`, will not raise error for a missing attribute - [@fixme](https://github.com/fixme).
* [#16](https://github.com/intridea/grape-entity/pull/16): Added `using` option to `expose SYMBOL BLOCK` - [@fahchen](https://github.com/fahchen).
* [#24](https://github.com/intridea/grape-entity/pull/24): Return documentation with `as` param considered - [@drakula2k](https://github.com/drakula2k).
* [#27](https://github.com/intridea/grape-entity/pull/27): Properly serialize hashes - [@clintonb](https://github.com/clintonb).
* [#28](https://github.com/intridea/grape-entity/pull/28): Look for method on entity before calling it on the object - [@MichaelXavier](https://github.com/MichaelXavier).
* [#33](https://github.com/intridea/grape-entity/pull/33): Support proper merging of nested conditionals - [@wyattisimo](https://github.com/wyattisimo).
* [#43](https://github.com/intridea/grape-entity/pull/43): Call procs in context of entity instance - [@joelvh](https://github.com/joelvh).
* Your contribution here.

0.3.0 (2013-03-29)
Expand Down
20 changes: 10 additions & 10 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def self.exposures
# the values are document keys in the entity's documentation key. When calling
# #docmentation, any exposure without a documentation key will be ignored.
def self.documentation
@documentation ||= exposures.inject({}) do |memo, value|
unless value[1][:documentation].nil? || value[1][:documentation].empty?
memo[key_for(value[0])] = value[1][:documentation]
@documentation ||= exposures.inject({}) do |memo, (attribute, exposure_options)|
unless exposure_options[:documentation].nil? || exposure_options[:documentation].empty?
memo[key_for(attribute)] = exposure_options[:documentation]
end
memo
end
Expand Down Expand Up @@ -286,7 +286,7 @@ def self.root(plural, singular = nil)
# even if one is defined for the entity.
def self.represent(objects, options = {})
if objects.respond_to?(:to_ary)
inner = objects.to_ary.map { |o| new(o, { collection: true }.merge(options)) }
inner = objects.to_ary.map { |object| new(object, { collection: true }.merge(options)) }
inner = inner.map(&:serializable_hash) if options[:serializable]
else
inner = new(objects, options)
Expand Down Expand Up @@ -380,9 +380,9 @@ def value_for(attribute, options = {})
using_options = options.dup
using_options.delete(:collection)
using_options[:root] = nil
exposure_options[:using].represent(exposure_options[:proc].call(object, options), using_options)
exposure_options[:using].represent(instance_exec(object, options, &exposure_options[:proc]), using_options)
else
exposure_options[:proc].call(object, options)
instance_exec(object, options, &exposure_options[:proc])
end
elsif exposure_options[:using]
using_options = options.dup
Expand All @@ -393,11 +393,11 @@ def value_for(attribute, options = {})
format_with = exposure_options[:format_with]

if format_with.is_a?(Symbol) && formatters[format_with]
formatters[format_with].call(delegate_attribute(attribute))
instance_exec(delegate_attribute(attribute), &formatters[format_with])
elsif format_with.is_a?(Symbol)
send(format_with, delegate_attribute(attribute))
elsif format_with.respond_to? :call
format_with.call(delegate_attribute(attribute))
instance_exec(delegate_attribute(attribute), &format_with)
end
else
delegate_attribute(attribute)
Expand Down Expand Up @@ -425,7 +425,7 @@ def conditions_met?(exposure_options, options)
if_conditions.each do |if_condition|
case if_condition
when Hash then if_condition.each_pair { |k, v| return false if options[k.to_sym] != v }
when Proc then return false unless if_condition.call(object, options)
when Proc then return false unless instance_exec(object, options, &if_condition)
when Symbol then return false unless options[if_condition]
end
end
Expand All @@ -436,7 +436,7 @@ def conditions_met?(exposure_options, options)
unless_conditions.each do |unless_condition|
case unless_condition
when Hash then unless_condition.each_pair { |k, v| return false if options[k.to_sym] == v }
when Proc then return false if unless_condition.call(object, options)
when Proc then return false if instance_exec(object, options, &unless_condition)
when Symbol then return false if options[unless_condition]
end
end
Expand Down
52 changes: 50 additions & 2 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@
subject.expose :name, using: 'Awesome', &block
subject.exposures[:name].should == { proc: block, using: 'Awesome' }
end

it 'references an instance of the entity without any options' do
subject.expose(:size) { self }
subject.represent(Hash.new).send(:value_for, :size).should be_an_instance_of fresh_class
end

it 'references an instance of the entity with :using option' do
module EntitySpec
class SomeObject1
attr_accessor :prop1

def initialize
@prop1 = "value1"
end
end

class BogusEntity < Grape::Entity
expose :prop1
end
end

subject.expose(:bogus, using: EntitySpec::BogusEntity) { self.object.prop1 = "MODIFIED 2"; self.object }

object = EntitySpec::SomeObject1.new
value = subject.represent(object).send(:value_for, :bogus)
value.should be_instance_of EntitySpec::BogusEntity

prop1 = value.send(:value_for, :prop1)
prop1.should == "MODIFIED 2"
end
end

context 'inherited exposures' do
Expand Down Expand Up @@ -101,6 +131,24 @@
model = { birthday: Time.gm(2012, 2, 27) }
subject.new(double(model)).as_json[:birthday].should == '02/27/2012'
end

it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
object = Hash.new

subject.expose(:size, format_with: lambda{|value| self.object.class.to_s})
subject.represent(object).send(:value_for, :size).should == object.class.to_s
end

it 'formats an exposure with a :format_with symbol that returns a value from the entity instance' do
subject.format_with :size_formatter do |date|
self.object.class.to_s
end

object = Hash.new

subject.expose(:size, format_with: :size_formatter)
subject.represent(object).send(:value_for, :size).should == object.class.to_s
end
end
end

Expand Down Expand Up @@ -190,11 +238,11 @@
it 'should override nested :using option' do
subject.class_eval do
with_options(using: 'Something') do
expose :awesome_thing, using: 'SometingElse'
expose :awesome_thing, using: 'SomethingElse'
end
end

subject.exposures[:awesome_thing].should == { using: 'SometingElse' }
subject.exposures[:awesome_thing].should == { using: 'SomethingElse' }
end

it 'should override nested :proc option' do
Expand Down