Class: JSONAPI::CachedResourceFragment
- Inherits:
-
Object
- Object
- JSONAPI::CachedResourceFragment
- Defined in:
- lib/jsonapi/cached_resource_fragment.rb
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.
-
#preloaded_fragments ⇒ Object
readonly
Returns the value of attribute preloaded_fragments.
-
#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
Instance Method Summary collapse
-
#initialize(resource_klass, id, type, context, fetchable_fields, relationships, links_json, attributes_json, meta_json) ⇒ CachedResourceFragment
constructor
A new instance of CachedResourceFragment.
- #to_cache_value ⇒ Object
- #to_real_resource ⇒ Object
Constructor Details
#initialize(resource_klass, id, type, context, fetchable_fields, relationships, links_json, attributes_json, meta_json) ⇒ CachedResourceFragment
Returns a new instance of CachedResourceFragment.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 36 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 @relationships = relationships @links_json = CompiledJson.of(links_json) @attributes_json = CompiledJson.of(attributes_json) @meta_json = CompiledJson.of() # A hash of hashes @preloaded_fragments ||= Hash.new end |
Instance Attribute Details
#attributes_json ⇒ Object (readonly)
Returns the value of attribute attributes_json.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def attributes_json @attributes_json end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def context @context end |
#fetchable_fields ⇒ Object (readonly)
Returns the value of attribute fetchable_fields.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def fetchable_fields @fetchable_fields end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def id @id end |
#links_json ⇒ Object (readonly)
Returns the value of attribute links_json.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def links_json @links_json end |
#meta_json ⇒ Object (readonly)
Returns the value of attribute meta_json.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def @meta_json end |
#preloaded_fragments ⇒ Object (readonly)
Returns the value of attribute preloaded_fragments.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def preloaded_fragments @preloaded_fragments end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def relationships @relationships end |
#resource_klass ⇒ Object (readonly)
Returns the value of attribute resource_klass.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def resource_klass @resource_klass end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
32 33 34 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 32 def type @type end |
Class Method Details
.fetch_fragments(resource_klass, serializer, context, cache_ids) ⇒ Object
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 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 3 def self.fetch_fragments(resource_klass, serializer, context, cache_ids) serializer_config_key = serializer.config_key(resource_klass).gsub("/", "_") context_json = resource_klass.attribute_caching_context(context).to_json context_b64 = JSONAPI.configuration.resource_cache_digest_function.call(context_json) context_key = "ATTR-CTX-#{context_b64.gsub("/", "_")}" results = self.lookup(resource_klass, serializer_config_key, context, context_key, cache_ids) miss_ids = results.select{|k,v| v.nil? }.keys unless miss_ids.empty? find_filters = {resource_klass._primary_key => miss_ids.uniq} = {context: context} resource_klass.find(find_filters, ).each do |resource| (id, cr) = write(resource_klass, resource, serializer, serializer_config_key, context, context_key) results[id] = cr end end if JSONAPI.configuration.resource_cache_usage_report_function JSONAPI.configuration.resource_cache_usage_report_function.call( resource_klass.name, cache_ids.size - miss_ids.size, miss_ids.size ) end return results end |
Instance Method Details
#to_cache_value ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 55 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 |
#to_real_resource ⇒ Object
67 68 69 70 |
# File 'lib/jsonapi/cached_resource_fragment.rb', line 67 def to_real_resource rs = Resource.resource_for(self.type).find_by_keys([self.id], {context: self.context}) return rs.try(:first) end |