-
Notifications
You must be signed in to change notification settings - Fork 534
/
Copy pathresource_fragment.rb
48 lines (39 loc) · 1.36 KB
/
resource_fragment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module JSONAPI
# 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
class ResourceFragment
attr_reader :identity, :attributes, :related_from, :related
attr_accessor :primary, :cache, :custom_cache
alias :cache_field :cache #ToDo: Rename one or the other
def initialize(identity)
@identity = identity
@cache = nil
@custom_cache = nil
@attributes = {}
@related = {}
@primary = false
@related_from = Set.new
end
def initialize_related(relationship_name)
@related ||= {}
@related[relationship_name.to_sym] ||= Set.new
end
def add_related_identity(relationship_name, identity)
initialize_related(relationship_name)
@related[relationship_name.to_sym] << identity
end
def add_related_from(identity)
@related_from << identity
end
def add_attribute(name, value)
@attributes[name] = value
end
end
end