class SimpleJSONAPIDeserializer::Resource

Attributes

cache[R]
includes[R]
resource[R]

Public Class Methods

new(resource, includes, cache) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 3
def initialize(resource, includes, cache)
  @resource = resource
  @includes = includes
  @cache = cache
end

Public Instance Methods

deserialize(without_attributes: false, without_relationships: false) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 9
def deserialize(without_attributes: false, without_relationships: false)
  {}.tap do |deserialized_resource|
    deserialized_resource.merge!(attributes) unless without_attributes
    deserialized_resource.merge!(deserialized_relationships) unless without_relationships
    deserialized_resource['type'] = type if type
    deserialized_resource['id'] = id if id
  end
end
id() click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 18
def id
  resource['id']
end
type() click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 22
def type
  resource['type']
end

Private Instance Methods

attributes() click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 30
def attributes
  resource['attributes'] || {}
end
deserialize_and_cache_relationship(relationship) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 69
def deserialize_and_cache_relationship(relationship)
  resource = Resource.new(relationship, includes, cache)

  write_to_cache(resource.id, resource.type, resource.deserialize(without_relationships: true))

  resource.deserialize.tap do |deserialized_resource|
    write_to_cache(resource.id, resource.type, deserialized_resource)
  end
end
deserialize_relationship(relationship) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 58
def deserialize_relationship(relationship)
  id = relationship['id']
  type = relationship['type']
  cached_resource = read_from_cache(id, type)
  included_resource = read_from_includes(id, type)

  return cached_resource if cached_resource

  deserialize_and_cache_relationship(included_resource || relationship)
end
deserialize_relationships(relationships) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 52
def deserialize_relationships(relationships)
  relationships.map do |relationship|
    deserialize_relationship(relationship)
  end
end
deserialized_relationships() click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 38
def deserialized_relationships
  {}.tap do |deserialized_relationships|
    relationships.each do |key, value|
      data = value['data']

      deserialized_relationships[key] = if data.is_a?(Array)
                                          deserialize_relationships(data)
                                        else
                                          deserialize_relationship(data)
                                        end
    end
  end
end
read_from_cache(id, type) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 87
def read_from_cache(id, type)
  cache.find(id, type)
end
read_from_includes(id, type) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 79
def read_from_includes(id, type)
  includes.find(id, type)
end
relationships() click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 34
def relationships
  resource['relationships'] || {}
end
write_to_cache(id, type, deserialized_resource) click to toggle source
# File lib/simple_jsonapi_deserializer/resource.rb, line 83
def write_to_cache(id, type, deserialized_resource)
  cache.cache(id, type, deserialized_resource)
end