Class: Shamu::JsonApi::Context
- Inherits:
-
Object
- Object
- Shamu::JsonApi::Context
- Defined in:
- lib/shamu/json_api/context.rb
Instance Method Summary collapse
-
#collect_included_resources ⇒ Array<Object,Hash>
Collects all the currently included resources and resets the queue.
-
#find_presenter(resource) ⇒ Class
Find a Presenter that can write the resource to a ResourceBuilder.
-
#include_field?(type, name, default = true) ⇒ Boolean
Check to see if the field should be included in the JSON API output.
-
#include_resource(resource, presenter = nil) {|builder| ... } ⇒ Object
Add an included resource for a compound response.
-
#included_resources? ⇒ Boolean
True if there are any pending included resources.
-
#initialize(fields: nil, namespaces: [], presenters: {}) ⇒ Context
constructor
A new instance of Context.
-
#params_meta ⇒ Hash
Of request param options to be output in the response meta.
Constructor Details
#initialize(fields: nil, namespaces: [], presenters: {}) ⇒ Context
Returns a new instance of Context.
19 20 21 22 23 24 25 |
# File 'lib/shamu/json_api/context.rb', line 19 def initialize( fields: nil, namespaces: [], presenters: {} ) @included_resources = {} @all_resources = Set.new @fields = parse_fields( fields ) @namespaces = Array( namespaces ) @presenters = presenters || {} end |
Instance Method Details
#collect_included_resources ⇒ Array<Object,Hash>
Collects all the currently included resources and resets the queue.
52 53 54 55 56 |
# File 'lib/shamu/json_api/context.rb', line 52 def collect_included_resources included = included_resources.dup @included_resources = {} included end |
#find_presenter(resource) ⇒ Class
Find a Presenter that can write the resource to a ResourceBuilder.
- First looks for any explicit presenter given to the constructor that maps the resource's class to a specific presenter.
- Next, looks through each of the namespaces given to the constructor.
For each namespace, looks for a
Namespace::#{ resource.class.name }Presenter
. Will also checkresource.class.model_name.name
if available. - Fails with a NoPresenter error if a presenter cannot be found.
89 90 91 92 93 94 95 96 |
# File 'lib/shamu/json_api/context.rb', line 89 def find_presenter( resource ) presenter = presenters[ resource.class ] presenter ||= presenters[ resource.class ] = find_namespace_presenter( resource ) fail NoPresenter.new( resource, namespaces ) unless presenter presenter end |
#include_field?(type, name, default = true) ⇒ Boolean
Check to see if the field should be included in the JSON API output.
70 71 72 73 74 |
# File 'lib/shamu/json_api/context.rb', line 70 def include_field?( type, name, default = true ) return default unless type_fields = fields[ type ] type_fields.include?( name ) end |
#include_resource(resource, presenter = nil) {|builder| ... } ⇒ Object
Add an included resource for a compound response.
If no presenter
and no block are provided a default presenter will be
obtained by calling #find_presenter.
38 39 40 41 42 43 44 45 46 |
# File 'lib/shamu/json_api/context.rb', line 38 def include_resource( resource, presenter = nil, &block ) return if all_resources.include?( resource ) all_resources << resource included_resources[resource] ||= begin presenter ||= find_presenter( resource ) unless block { presenter: presenter, block: block } end end |
#included_resources? ⇒ Boolean
Returns true if there are any pending included resources.
59 60 61 |
# File 'lib/shamu/json_api/context.rb', line 59 def included_resources? included_resources.any? end |
#params_meta ⇒ Hash
Returns of request param options to be output in the response meta.
99 100 101 102 103 |
# File 'lib/shamu/json_api/context.rb', line 99 def return unless fields.any? { fields: fields } end |