class LightSerializer::HashedObject

Constants

UnknownAttribute

Attributes

raw_object[R]
serializer[R]

Public Class Methods

get(*args) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 11
def self.get(*args)
  new(*args).get
end
new(raw_object, serializer) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 15
def initialize(raw_object, serializer)
  @raw_object = raw_object
  @serializer = serializer
end

Public Instance Methods

get() click to toggle source
# File lib/light_serializer/hashed_object.rb, line 20
def get
  with_custom_root(serializer.root) { transform_to_hash(raw_object) }
end

Private Instance Methods

active_record_relation?(object) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 57
def active_record_relation?(object)
  defined?(ActiveRecord::Relation) && object.is_a?(ActiveRecord::Relation)
end
can_be_enumerated?(value) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 53
def can_be_enumerated?(value)
  value.is_a?(Enumerable) || active_record_relation?(value)
end
hashed_entity(entity, nested_serializer) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 61
def hashed_entity(entity, nested_serializer)
  nested_serializer.new(entity, context: serializer.context).to_hash
end
hashed_nested_resource(value, nested_serializer) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 47
def hashed_nested_resource(value, nested_serializer)
  return hashed_entity(value, nested_serializer) unless can_be_enumerated?(value)

  value.each_with_object(nested_serializer).map { |entity, serializer| hashed_entity(entity, serializer) }
end
obtain_value(object, attribute, nested_serializer = nil) click to toggle source

:reek: ManualDispatch

# File lib/light_serializer/hashed_object.rb, line 76
def obtain_value(object, attribute, nested_serializer = nil)
  if serializer.respond_to?(attribute)
    serializer.public_send(attribute)
  elsif object.respond_to?(attribute)
    object.public_send(attribute)
  elsif raw_object != object
    object
  elsif !nested_serializer
    raise UnknownAttribute, "Unknown attribute: #{attribute}"
  end
end
obtain_values(attribute, object, result) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 32
def obtain_values(attribute, object, result)
  if attribute.is_a?(Hash)
    values_from_nested_resource(attribute, object, result)
  else
    values_from_current_resource(attribute, object, result)
  end
end
transform_to_hash(object) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 26
def transform_to_hash(object)
  serializer.class.attributes.each_with_object({}) do |attribute, result|
    obtain_values(attribute, object, result)
  end
end
values_from_current_resource(attribute, object, result) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 65
def values_from_current_resource(attribute, object, result)
  value = obtain_value(object, attribute)

  result[attribute] = if value.is_a?(Array)
    values_from_for_array(attribute, value, result)
  else
    value
  end
end
values_from_for_array(attribute, value, result) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 88
def values_from_for_array(attribute, value, result)
  value.map { |entity| entity.is_a?(Hash) ? entity : obtain_values(attribute, entity, result) }
end
values_from_nested_resource(attributes, object, result) click to toggle source
# File lib/light_serializer/hashed_object.rb, line 40
def values_from_nested_resource(attributes, object, result)
  attributes.each do |attribute_name, nested_serializer|
    value = obtain_value(object, attribute_name, nested_serializer)
    result[attribute_name] = hashed_nested_resource(value, nested_serializer)
  end
end