Skip to content

Commit 44dd623

Browse files
author
Philip Champon
committed
possibly improved docs
1 parent 04d7efd commit 44dd623

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

README.markdown

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -445,32 +445,34 @@ end
445445

446446
## Reusable Responses with Entities
447447

448-
Entities are a simple and reusable means for converting Ruby objects to API responses.
448+
Entities are a reusable means for converting Ruby objects to API responses.
449449
Entities can be used to conditionally include fields, nest other entities, and build
450450
ever larger responses, using inheritance.
451451

452452
### Defining Entities
453453

454-
Entities inherit from Grape::Entity, and define a simple DSL. The `#expose` method can be called in
455-
a number of ways. When processing options passed to exposures two keys will always be defined, :version
456-
and :collection. The :version key is define as api.version. The :collection key is boolean, and defined
457-
as true if the object presented is an array.
454+
Entities inherit from Grape::Entity, and define a simple DSL. Exposures can use
455+
runtime options to determine which fields should be visible, these options are
456+
available to :if, :unless, and :proc. The option keys :version and :collection
457+
will always be defined. The :version key is defined as api.version. The
458+
:collection key is boolean, and defined as true if the object presented is an
459+
array.
458460

459-
* expose SYMBOLS
461+
* `expose SYMBOLS`
460462
* define a list of fields which will always be exposed
461-
* expose SYMBOLS, HASH
463+
* `expose SYMBOLS, HASH`
462464
* HASH keys include :if, :unless, :proc, :as, :using, :format_with, :documentation
463465
* :if and :unless accept hashes (passed during runtime) or procs (arguments are object and options)
464-
* expose SYMBOL, {:format_with => :formatter}
466+
* `expose SYMBOL, {:format_with => :formatter}`
465467
* expose a value, formatting it first
466-
* `:format_with` can only be applied to one exposure at a time
467-
* expose SYMBOL, {:as => "alias"}
468+
* :format_with can only be applied to one exposure at a time
469+
* `expose SYMBOL, {:as => "alias"}`
468470
* Expose a value, changing its hash key from SYMBOL to alias
469-
* `:as` can only be applied to one exposure at a time
470-
* expose SYMBOL BLOCK
471+
* :as can only be applied to one exposure at a time
472+
* `expose SYMBOL BLOCK`
471473
* block arguments are object and options
472474
* expose the value returned by the block
473-
* `block` can only be applied to one exposure at a time
475+
* block can only be applied to one exposure at a time
474476

475477
``` ruby
476478
module API

lib/grape/entity.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
module Grape
44
# An Entity is a lightweight structure that allows you to easily
55
# represent data from your application in a consistent and abstracted
6-
# way in your API.
6+
# way in your API. Entities can also provide documentation for the
7+
# fields exposed.
78
#
89
# @example Entity Definition
910
#
1011
# module API
1112
# module Entities
1213
# class User < Grape::Entity
1314
# expose :first_name, :last_name, :screen_name, :location
15+
# expose :field, :documentation => {:type => "string", :desc => "describe the field"}
1416
# expose :latest_status, :using => API::Status, :as => :status, :unless => {:collection => true}
1517
# expose :email, :if => {:type => :full}
1618
# expose :new_attribute, :if => {:version => 'v2'}
@@ -30,6 +32,7 @@ module Grape
3032
# class Users < Grape::API
3133
# version 'v2'
3234
#
35+
# desc 'User index', { :object_fields => API::Entities::User.documentation }
3336
# get '/users' do
3437
# @users = User.all
3538
# type = current_user.admin? ? :full : :default
@@ -63,11 +66,8 @@ class Entity
6366
# will be called with the represented object as well as the
6467
# runtime options that were passed in. You can also just supply a
6568
# block to the expose call to achieve the same effect.
66-
# @option options :documentation Provide documenation for an exposed
69+
# @option options :documentation Define documenation for an exposed
6770
# field, typically the value is a hash with two fields, type and desc.
68-
# Call the class or instance method #documentation for use with #desc.
69-
# When calling #docmentation, any exposure without a documentation key
70-
# will be ignored.
7171
def self.expose(*args, &block)
7272
options = args.last.is_a?(Hash) ? args.pop : {}
7373

@@ -98,9 +98,9 @@ def self.exposures
9898
@exposures
9999
end
100100

101-
# Returns a hash of documentation hashes that have been declared for this Entity or ancestors. The keys
102-
# are symbolized references to fields in the response, the values are those defined in the
103-
# Entity's documentation key.
101+
# Returns a hash, the keys are symbolized references to fields in the entity,
102+
# the values are document keys in the entity's documentation key. When calling
103+
# #docmentation, any exposure without a documentation key will be ignored.
104104
def self.documentation
105105
@documentation ||= exposures.inject({}) do |memo, value|
106106
unless value[1][:documentation].nil? || value[1][:documentation].empty?

0 commit comments

Comments
 (0)