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 resource - a resource instance

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, resource: nil, cache: nil, primary: false) ⇒ ResourceFragment

Returns a new instance of ResourceFragment.



23
24
25
26
27
28
29
30
31
32
# File 'lib/jsonapi/resource_fragment.rb', line 23

def initialize(identity, resource: nil, cache: nil, primary: false)
  @identity = identity
  @cache = cache
  @resource = resource
  @primary = primary

  @attributes = {}
  @related = {}
  @related_from = Set.new
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#cacheObject Also known as: cache_field

Returns the value of attribute cache.



19
20
21
# File 'lib/jsonapi/resource_fragment.rb', line 19

def cache
  @cache
end

#identityObject (readonly)

Returns the value of attribute identity.



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

def identity
  @identity
end

#primaryObject

Returns the value of attribute primary.



19
20
21
# File 'lib/jsonapi/resource_fragment.rb', line 19

def primary
  @primary
end

Returns the value of attribute related.



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

def related
  @related
end

Returns the value of attribute related_from.



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

def related_from
  @related_from
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Instance Method Details

#add_attribute(name, value) ⇒ Object



52
53
54
# File 'lib/jsonapi/resource_fragment.rb', line 52

def add_attribute(name, value)
  @attributes[name] = value
end


48
49
50
# File 'lib/jsonapi/resource_fragment.rb', line 48

def add_related_from(identity)
  @related_from << identity
end


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

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


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

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


43
44
45
46
# File 'lib/jsonapi/resource_fragment.rb', line 43

def merge_related_identities(relationship_name, identities)
  initialize_related(relationship_name)
  @related[relationship_name.to_sym].merge(identities) if identities
end