Class: JSONAPI::ResourceTree
- Inherits:
-
Object
- Object
- JSONAPI::ResourceTree
- Defined in:
- lib/jsonapi/resource_tree.rb
Overview
A tree structure representing the resource structure of the requested resource(s). This is an intermediate structure used to keep track of the resources, by identity, found at different included relationships. It will be flattened and the resource instances will be fetched from the cache or the record store.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fragments ⇒ Object
readonly
Returns the value of attribute fragments.
-
#related_resource_trees ⇒ Object
readonly
Returns the value of attribute related_resource_trees.
Instance Method Summary collapse
-
#add_resource(resource, include_related) ⇒ null
Adds a Resource to the fragments hash.
-
#add_resource_fragment(fragment, include_related) ⇒ null
Adds a Resource Fragment to the fragments hash.
-
#add_resource_fragments(fragments, include_related) ⇒ null
Adds each Resource Fragment to the Resources hash.
-
#add_resources(resources, include_related) ⇒ null
Adds each Resource to the fragments hash.
-
#get_related_resource_tree(relationship) ⇒ JSONAPI::RelatedResourceTree
Gets the related Resource Id Tree for a relationship, and creates it first if it does not exist.
Instance Attribute Details
#fragments ⇒ Object (readonly)
Returns the value of attribute fragments.
10 11 12 |
# File 'lib/jsonapi/resource_tree.rb', line 10 def fragments @fragments end |
#related_resource_trees ⇒ Object (readonly)
Returns the value of attribute related_resource_trees.
10 11 12 |
# File 'lib/jsonapi/resource_tree.rb', line 10 def @related_resource_trees end |
Instance Method Details
#add_resource(resource, include_related) ⇒ null
Adds a Resource to the fragments hash
64 65 66 |
# File 'lib/jsonapi/resource_tree.rb', line 64 def add_resource(resource, ) add_resource_fragment(JSONAPI::ResourceFragment.new(resource.identity, resource: resource), ) end |
#add_resource_fragment(fragment, include_related) ⇒ null
Adds a Resource Fragment to the fragments hash
40 41 42 43 44 |
# File 'lib/jsonapi/resource_tree.rb', line 40 def add_resource_fragment(fragment, ) init_included_relationships(fragment, ) @fragments[fragment.identity] = fragment end |
#add_resource_fragments(fragments, include_related) ⇒ null
Adds each Resource Fragment to the Resources hash
28 29 30 31 32 |
# File 'lib/jsonapi/resource_tree.rb', line 28 def add_resource_fragments(fragments, ) fragments.each_value do |fragment| add_resource_fragment(fragment, ) end end |
#add_resources(resources, include_related) ⇒ null
Adds each Resource to the fragments hash
52 53 54 55 56 |
# File 'lib/jsonapi/resource_tree.rb', line 52 def add_resources(resources, ) resources.each do |resource| add_resource_fragment(JSONAPI::ResourceFragment.new(resource.identity, resource: resource), ) end end |
#get_related_resource_tree(relationship) ⇒ JSONAPI::RelatedResourceTree
Gets the related Resource Id Tree for a relationship, and creates it first if it does not exist
17 18 19 20 |
# File 'lib/jsonapi/resource_tree.rb', line 17 def (relationship) relationship_name = relationship.name.to_sym @related_resource_trees[relationship_name] ||= RelatedResourceTree.new(relationship, self) end |