Class: JSONAPI::CachedResponseFragment
- Inherits:
-
Object
- Object
- JSONAPI::CachedResponseFragment
- Defined in:
- lib/jsonapi/cached_response_fragment.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#attributes_json ⇒ Object
readonly
Returns the value of attribute attributes_json.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#fetchable_fields ⇒ Object
readonly
Returns the value of attribute fetchable_fields.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#links_json ⇒ Object
readonly
Returns the value of attribute links_json.
-
#meta_json ⇒ Object
readonly
Returns the value of attribute meta_json.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
-
#resource_klass ⇒ Object
readonly
Returns the value of attribute resource_klass.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .from_cache_value(resource_klass, context, h) ⇒ Object
- .lookup(lookups, context) ⇒ Hash<Class<Resource>, Hash<ID, CachedResourceFragment>>
- .write(writes) ⇒ Object
Instance Method Summary collapse
-
#initialize(resource_klass, id, type, context, fetchable_fields, relationships, links_json, attributes_json, meta_json) ⇒ CachedResponseFragment
constructor
A new instance of CachedResponseFragment.
- #to_cache_value ⇒ Object
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, ) @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() end |
Instance Attribute Details
#attributes_json ⇒ Object (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 |
#context ⇒ Object (readonly)
Returns the value of attribute context.
42 43 44 |
# File 'lib/jsonapi/cached_response_fragment.rb', line 42 def context @context end |
#fetchable_fields ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
42 43 44 |
# File 'lib/jsonapi/cached_response_fragment.rb', line 42 def id @id end |
#links_json ⇒ Object (readonly)
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_json ⇒ Object (readonly)
Returns the value of attribute meta_json.
42 43 44 |
# File 'lib/jsonapi/cached_response_fragment.rb', line 42 def @meta_json end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
42 43 44 |
# File 'lib/jsonapi/cached_response_fragment.rb', line 42 def relationships @relationships end |
#resource_klass ⇒ Object (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 |
#type ⇒ Object (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>>
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
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_value ⇒ Object
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: .try(:to_s) } end |