Skip to content

Commit 1d88ac3

Browse files
author
Philip Champon
committed
Added documentation to entities
1 parent f8d280b commit 1d88ac3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/grape/entity.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ def self.exposures
9393
@exposures
9494
end
9595

96+
# Returns a hash of documentation hashes that have been declared for this Entity or ancestors. The keys
97+
# are symbolized references to methods on the containing object, the values are
98+
# the options that were passed into expose.
99+
def self.documentation
100+
@documentation ||= exposures.inject({}) do |memo, value|
101+
unless value[1][:documentation].nil? || value[1][:documentation].empty?
102+
memo[value[0]] = value[1][:documentation]
103+
end
104+
memo
105+
end
106+
107+
if superclass.respond_to? :documentation
108+
@documentation = superclass.documentation.merge(@documentation)
109+
end
110+
111+
@documentation
112+
end
113+
96114
# This allows you to declare a Proc in which exposures can be formatted with.
97115
# It take a block with an arity of 1 which is passed as the value of the exposed attribute.
98116
#
@@ -217,6 +235,10 @@ def exposures
217235
self.class.exposures
218236
end
219237

238+
def documentation
239+
self.class.documentation
240+
end
241+
220242
def formatters
221243
self.class.formatters
222244
end

spec/grape/entity_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,23 @@ class FriendEntity < Grape::Entity
317317
end
318318
end
319319

320+
describe '#documentation' do
321+
it 'should return an empty hash is no documentation is provided' do
322+
fresh_class.expose :name
323+
324+
subject.documentation.should == {}
325+
end
326+
327+
it 'should return each defined documentation hash' do
328+
doc = {:type => "foo", :desc => "bar"}
329+
fresh_class.expose :name, :documentation => doc
330+
fresh_class.expose :email, :documentation => doc
331+
fresh_class.expose :birthday
332+
333+
subject.documentation.should == {:name => doc, :email => doc}
334+
end
335+
end
336+
320337
describe '#key_for' do
321338
it 'should return the attribute if no :as is set' do
322339
fresh_class.expose :name

0 commit comments

Comments
 (0)