Class: JSONAPI::CachedResponseFragment

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/cached_response_fragment.rb

Defined Under Namespace

Classes: Lookup, Write

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, id, type, context, fetchable_fields, relationships, links_json, attributes_json, meta_json) ⇒ CachedResponseFragment

Returns a new instance of CachedResponseFragment.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jsonapi/cached_response_fragment.rb', line 45

def initialize(resource_klass, id, type, context, fetchable_fields, relationships,
               links_json, attributes_json, meta_json)
  @resource_klass = resource_klass
  @id = id
  @type = type
  @context = context
  @fetchable_fields = Set.new(fetchable_fields)

  # Relationships left uncompiled because we'll often want to insert included ids on retrieval
  # Remove the data since that should not be cached
  @relationships = relationships&.transform_values {|v| v.delete_if {|k, _v| k == 'data'} }
  @links_json = CompiledJson.of(links_json)
  @attributes_json = CompiledJson.of(attributes_json)
  @meta_json = CompiledJson.of(meta_json)
end

Instance Attribute Details

#attributes_jsonObject (readonly)

Returns the value of attribute attributes_json.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def attributes_json
  @attributes_json
end

#contextObject (readonly)

Returns the value of attribute context.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def context
  @context
end

#fetchable_fieldsObject (readonly)

Returns the value of attribute fetchable_fields.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def fetchable_fields
  @fetchable_fields
end

#idObject (readonly)

Returns the value of attribute id.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def id
  @id
end

Returns the value of attribute links_json.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def links_json
  @links_json
end

#meta_jsonObject (readonly)

Returns the value of attribute meta_json.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def meta_json
  @meta_json
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def relationships
  @relationships
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def resource_klass
  @resource_klass
end

#typeObject (readonly)

Returns the value of attribute type.



42
43
44
# File 'lib/jsonapi/cached_response_fragment.rb', line 42

def type
  @type
end

Class Method Details

.from_cache_value(resource_klass, context, h) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jsonapi/cached_response_fragment.rb', line 113

def self.from_cache_value(resource_klass, context, h)
  new(
    resource_klass,
    h.fetch(:id),
    h.fetch(:type),
    context,
    h.fetch(:fetchable),
    h.fetch(:rels, nil),
    h.fetch(:links, nil),
    h.fetch(:attrs, nil),
    h.fetch(:meta, nil)
  )
end

.lookup(lookups, context) ⇒ Hash<Class<Resource>, Hash<ID, CachedResourceFragment>>

Parameters:

Returns:

  • (Hash<Class<Resource>, Hash<ID, CachedResourceFragment>>)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jsonapi/cached_response_fragment.rb', line 75

def self.lookup(lookups, context)
  type_to_klass = lookups.map {|l| [l.type, l.resource_klass]}.to_h

  keys = lookups.map(&:keys).flatten(1)

  hits = JSONAPI.configuration.resource_cache.read_multi(*keys).reject {|_, v| v.nil?}

  return keys.inject({}) do |hash, key|
    (type, id, _, _) = key
    resource_klass = type_to_klass[type]
    hash[resource_klass] ||= {}

    if hits.has_key?(key)
      hash[resource_klass][id] = self.from_cache_value(resource_klass, context, hits[key])
    else
      hash[resource_klass][id] = nil
    end

    hash
  end
end

.write(writes) ⇒ Object

Parameters:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jsonapi/cached_response_fragment.rb', line 98

def self.write(writes)
  key_values = writes.map(&:to_key_value)

  to_write = key_values.map {|(k, v)| [k, v.to_cache_value]}.to_h

  if JSONAPI.configuration.resource_cache.respond_to? :write_multi
    JSONAPI.configuration.resource_cache.write_multi(to_write)
  else
    to_write.each do |key, value|
      JSONAPI.configuration.resource_cache.write(key, value)
    end
  end

end

Instance Method Details

#to_cache_valueObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jsonapi/cached_response_fragment.rb', line 61

def to_cache_value
  {
    id: id,
    type: type,
    fetchable: fetchable_fields,
    rels: relationships,
    links: links_json.try(:to_s),
    attrs: attributes_json.try(:to_s),
    meta: meta_json.try(:to_s)
  }
end