module Hat::Sideloading::JsonSerializer

Attributes

attribute_mappings[R]
cached[R]
has_many_mappings[R]
has_one_mappings[R]
relation_includes[R]
relation_sideloader[R]
result[R]

Public Class Methods

new(serializable, attrs = {}) click to toggle source
Calls superclass method Hat::BaseJsonSerializer::new
# File lib/hat/sideloading/json_serializer.rb, line 13
def initialize(serializable, attrs = {})
  super
  @identity_map = attrs[:identity_map] || IdentityMap.new
  @type_key_resolver = attrs[:type_key_resolver] || TypeKeyResolver.new
end

Private Class Methods

included(serializer_class) click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 118
def self.included(serializer_class)
  JsonSerializerDsl.apply_to(serializer_class)
end

Public Instance Methods

as_json(*args) click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 19
def as_json(*args)

  result[root_key] = []

  Array(serializable).each do |serializable_item|
    serialize_item_and_cache_relationships(serializable_item)
  end

  result.merge({ meta: meta_data.merge(includes_meta_data), linked: relation_sideloader.as_json })
end
as_sideloaded_hash() click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 30
def as_sideloaded_hash
  hash = attribute_hash.merge(links: has_one_hash.merge(has_many_hash))
  relation_sideloader.sideload_relations
  hash
end

Private Instance Methods

has_many_hash() click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 77
def has_many_hash

  has_many_hash = {}

  has_manys.each do |attr_name|
    has_many_relation = serializable.send(attr_name) || []
    has_many_hash[attr_name] = has_many_relation.map(&:id)
  end

  has_many_hash

end
has_one_hash() click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 53
def has_one_hash

  has_one_hash = {}

  has_ones.each do |attr_name|

    id_attr = "#{attr_name}_id"

    #Use id attr if possible as it's cheaper than referencing the object
    if serializable.respond_to?(id_attr)
      related_id = serializable.send(id_attr)
    else
      related_id = serializable.send(attr_name).try(:id)
    end

    has_one_hash[attr_name] = related_id

  end

  has_one_hash

end
serialize_item_and_cache_relationships(serializable_item) click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 40
def serialize_item_and_cache_relationships(serializable_item)

  assert_id_present(serializable_item)

  serializer = serializer_for(serializable_item)
  hashed = { id: serializable_item.id }

  result[root_key] << hashed

  hashed.merge!(serializer.includes(*relation_includes.to_a).as_sideloaded_hash)

end
serializer_class_for(serializable) click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 114
def serializer_class_for(serializable)
  Hat::SerializerRegistry.instance.get(:sideloading, serializable.class)
end
serializer_for(serializable_item) click to toggle source
# File lib/hat/sideloading/json_serializer.rb, line 102
def serializer_for(serializable_item)

  serializer_class = serializer_class_for(serializable_item)

  serializer_class.new(serializable_item, {
    result: result,
    identity_map: identity_map,
    type_key_resolver: type_key_resolver
  })

end