Class: JSONAPI::ResourceFragment

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



14
15
16
# File 'lib/jsonapi/resource_fragment.rb', line 14

def attributes
  @attributes
end

#cacheObject 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

#identityObject (readonly)

Returns the value of attribute identity.



14
15
16
# File 'lib/jsonapi/resource_fragment.rb', line 14

def identity
  @identity
end

#primaryObject

Returns the value of attribute primary.



16
17
18
# File 'lib/jsonapi/resource_fragment.rb', line 16

def primary
  @primary
end

Returns the value of attribute related.



14
15
16
# File 'lib/jsonapi/resource_fragment.rb', line 14

def related
  @related
end

Returns the value of attribute related_from.



14
15
16
# File 'lib/jsonapi/resource_fragment.rb', line 14

def related_from
  @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


39
40
41
# File 'lib/jsonapi/resource_fragment.rb', line 39

def add_related_from(identity)
  @related_from << identity
end


34
35
36
37
# File 'lib/jsonapi/resource_fragment.rb', line 34

def add_related_identity(relationship_name, identity)
  initialize_related(relationship_name)
  @related[relationship_name.to_sym] << identity
end


29
30
31
32
# File 'lib/jsonapi/resource_fragment.rb', line 29

def initialize_related(relationship_name)
  @related ||= {}
  @related[relationship_name.to_sym] ||= Set.new
end