Class: JSONAPI::ResourceFragment
- Inherits:
-
Object
- Object
- JSONAPI::ResourceFragment
- Defined in:
- lib/jsonapi/resource_fragment.rb
Overview
A ResourceFragment holds a ResourceIdentity and associated partial resource data.
The following partial resource data may be stored cache - the value of the cache field for the resource instance related - a hash of arrays of related resource identities, grouped by relationship name related_from - a set of related resource identities that loaded the fragment
Todo: optionally use these for faster responses by bypassing model instantiation) attributes - resource attributes
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#cache ⇒ Object
(also: #cache_field)
Returns the value of attribute cache.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#primary ⇒ Object
Returns the value of attribute primary.
-
#related ⇒ Object
readonly
Returns the value of attribute related.
-
#related_from ⇒ Object
readonly
Returns the value of attribute related_from.
Instance Method Summary collapse
- #add_attribute(name, value) ⇒ Object
- #add_related_from(identity) ⇒ Object
- #add_related_identity(relationship_name, identity) ⇒ Object
-
#initialize(identity) ⇒ ResourceFragment
constructor
A new instance of ResourceFragment.
- #initialize_related(relationship_name) ⇒ Object
Constructor Details
#initialize(identity) ⇒ ResourceFragment
Returns a new instance of ResourceFragment.
20 21 22 23 24 25 26 27 |
# File 'lib/jsonapi/resource_fragment.rb', line 20 def initialize(identity) @identity = identity @cache = nil @attributes = {} @related = {} @primary = false @related_from = Set.new end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
14 15 16 |
# File 'lib/jsonapi/resource_fragment.rb', line 14 def attributes @attributes end |
#cache ⇒ Object Also known as: cache_field
Returns the value of attribute cache.
16 17 18 |
# File 'lib/jsonapi/resource_fragment.rb', line 16 def cache @cache end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
14 15 16 |
# File 'lib/jsonapi/resource_fragment.rb', line 14 def identity @identity end |
#primary ⇒ Object
Returns the value of attribute primary.
16 17 18 |
# File 'lib/jsonapi/resource_fragment.rb', line 16 def primary @primary end |
#related ⇒ Object (readonly)
Returns the value of attribute related.
14 15 16 |
# File 'lib/jsonapi/resource_fragment.rb', line 14 def @related end |
#related_from ⇒ Object (readonly)
Returns the value of attribute related_from.
14 15 16 |
# File 'lib/jsonapi/resource_fragment.rb', line 14 def @related_from end |
Instance Method Details
#add_attribute(name, value) ⇒ Object
43 44 45 |
# File 'lib/jsonapi/resource_fragment.rb', line 43 def add_attribute(name, value) @attributes[name] = value end |
#add_related_from(identity) ⇒ Object
39 40 41 |
# File 'lib/jsonapi/resource_fragment.rb', line 39 def (identity) @related_from << identity end |
#add_related_identity(relationship_name, identity) ⇒ Object
34 35 36 37 |
# File 'lib/jsonapi/resource_fragment.rb', line 34 def (relationship_name, identity) (relationship_name) @related[relationship_name.to_sym] << identity end |
#initialize_related(relationship_name) ⇒ Object
29 30 31 32 |
# File 'lib/jsonapi/resource_fragment.rb', line 29 def (relationship_name) @related ||= {} @related[relationship_name.to_sym] ||= Set.new end |