Skip to content

Commit 0c61c53

Browse files
committed
Merge pull request #24 from smtpcom/master
Return documentation with :as param considering
2 parents 4b02356 + d3c6eaa commit 0c61c53

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

CHANGELOG.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Next Release
44
* [#7](https://github.com/intridea/grape-entity/issues/7): Add `serializable` option to `represent` - [@mbleigh](https://github.com/mbleigh).
55
* [#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).
66
* [#16](https://github.com/intridea/grape-entity/pull/16): Add `:using` option to `expose SYMBOL BLOCK` - [@fahchen](https://github.com/fahchen).
7+
* [#24](https://github.com/intridea/grape-entity/pull/24): Return documentation with `:as` param considering - [@drakula2k](https://github.com/drakula2k).
78
* Your contribution here.
89

910
0.3.0 (2013-03-29)

lib/grape_entity/entity.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def self.exposures
174174
def self.documentation
175175
@documentation ||= exposures.inject({}) do |memo, value|
176176
unless value[1][:documentation].nil? || value[1][:documentation].empty?
177-
memo[value[0]] = value[1][:documentation]
177+
memo[key_for(value[0])] = value[1][:documentation]
178178
end
179179
memo
180180
end
@@ -339,7 +339,7 @@ def serializable_hash(runtime_options = {})
339339
valid_exposures.inject({}) do |output, (attribute, exposure_options)|
340340
if conditions_met?(exposure_options, opts)
341341
partial_output = value_for(attribute, opts)
342-
output[key_for(attribute)] =
342+
output[self.class.key_for(attribute)] =
343343
if partial_output.respond_to? :serializable_hash
344344
partial_output.serializable_hash(runtime_options)
345345
elsif partial_output.kind_of?(Array) && !partial_output.map {|o| o.respond_to? :serializable_hash}.include?(false)
@@ -366,7 +366,7 @@ def to_xml(options = {})
366366

367367
protected
368368

369-
def key_for(attribute)
369+
def self.key_for(attribute)
370370
exposures[attribute.to_sym][:as] || attribute.to_sym
371371
end
372372

spec/grape_entity/entity_spec.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -605,22 +605,31 @@ class FriendEntity < Grape::Entity
605605

606606
subject.documentation.should == {:name => doc, :email => doc}
607607
end
608+
609+
it 'returns each defined documentation hash with :as param considering' do
610+
doc = {:type => "foo", :desc => "bar"}
611+
fresh_class.expose :name, :documentation => doc, :as => :label
612+
fresh_class.expose :email, :documentation => doc
613+
fresh_class.expose :birthday
614+
615+
subject.documentation.should == {:label => doc, :email => doc}
616+
end
608617
end
609618

610619
describe '#key_for' do
611620
it 'returns the attribute if no :as is set' do
612621
fresh_class.expose :name
613-
subject.send(:key_for, :name).should == :name
622+
subject.class.send(:key_for, :name).should == :name
614623
end
615624

616625
it 'returns a symbolized version of the attribute' do
617626
fresh_class.expose :name
618-
subject.send(:key_for, 'name').should == :name
627+
subject.class.send(:key_for, 'name').should == :name
619628
end
620629

621630
it 'returns the :as alias if one exists' do
622631
fresh_class.expose :name, :as => :nombre
623-
subject.send(:key_for, 'name').should == :nombre
632+
subject.class.send(:key_for, 'name').should == :nombre
624633
end
625634
end
626635

0 commit comments

Comments
 (0)