Skip to content

Commit

Permalink
Merge pull request #24 from smtpcom/master
Browse files Browse the repository at this point in the history
Return documentation with :as param considering
  • Loading branch information
dblock committed Sep 15, 2013
2 parents 4b02356 + d3c6eaa commit 0c61c53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Next Release
* [#7](https://github.com/intridea/grape-entity/issues/7): Add `serializable` option to `represent` - [@mbleigh](https://github.com/mbleigh).
* [#18](https://github.com/intridea/grape-entity/pull/18): Add `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): Add `: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 considering - [@drakula2k](https://github.com/drakula2k).
* Your contribution here.

0.3.0 (2013-03-29)
Expand Down
6 changes: 3 additions & 3 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def self.exposures
def self.documentation
@documentation ||= exposures.inject({}) do |memo, value|
unless value[1][:documentation].nil? || value[1][:documentation].empty?
memo[value[0]] = value[1][:documentation]
memo[key_for(value[0])] = value[1][:documentation]
end
memo
end
Expand Down Expand Up @@ -339,7 +339,7 @@ def serializable_hash(runtime_options = {})
valid_exposures.inject({}) do |output, (attribute, exposure_options)|
if conditions_met?(exposure_options, opts)
partial_output = value_for(attribute, opts)
output[key_for(attribute)] =
output[self.class.key_for(attribute)] =
if partial_output.respond_to? :serializable_hash
partial_output.serializable_hash(runtime_options)
elsif partial_output.kind_of?(Array) && !partial_output.map {|o| o.respond_to? :serializable_hash}.include?(false)
Expand All @@ -366,7 +366,7 @@ def to_xml(options = {})

protected

def key_for(attribute)
def self.key_for(attribute)
exposures[attribute.to_sym][:as] || attribute.to_sym
end

Expand Down
15 changes: 12 additions & 3 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -605,22 +605,31 @@ class FriendEntity < Grape::Entity

subject.documentation.should == {:name => doc, :email => doc}
end

it 'returns each defined documentation hash with :as param considering' do
doc = {:type => "foo", :desc => "bar"}
fresh_class.expose :name, :documentation => doc, :as => :label
fresh_class.expose :email, :documentation => doc
fresh_class.expose :birthday

subject.documentation.should == {:label => doc, :email => doc}
end
end

describe '#key_for' do
it 'returns the attribute if no :as is set' do
fresh_class.expose :name
subject.send(:key_for, :name).should == :name
subject.class.send(:key_for, :name).should == :name
end

it 'returns a symbolized version of the attribute' do
fresh_class.expose :name
subject.send(:key_for, 'name').should == :name
subject.class.send(:key_for, 'name').should == :name
end

it 'returns the :as alias if one exists' do
fresh_class.expose :name, :as => :nombre
subject.send(:key_for, 'name').should == :nombre
subject.class.send(:key_for, 'name').should == :nombre
end
end

Expand Down

0 comments on commit 0c61c53

Please sign in to comment.