class ActiveSerializer::Serializers::HashSerializer

Attributes

serialized_data[R]

Public Class Methods

new(root_hash, options = {}) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 4
def initialize(root_hash, options = {})
  @root_hash       = root_hash
  @options         = options
  @serialized_data = {}
end

Public Instance Methods

attribute(name, value = nil) { || ... } click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 10
def attribute(name, value = nil, &block)
  if block_given?
    value = yield
  elsif !value
    value = @root_hash[name]
  end
  set_value(name, value)
end
attributes(*serialized_data, &block) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 32
def attributes(*serialized_data, &block)
  if !serialized_data.last.is_a?(Symbol)
    source_hash = serialized_data.delete_at(-1)
  else
    source_hash = @root_hash
  end

  serialized_data.each do |attr_name|
    set_value(attr_name, source_hash[attr_name])
  end
end
namespace(name, &block) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 44
def namespace(name, &block)
  serializer = self.class.new(@root_hash, @options)
  serializer.instance_exec(@root_hash, &block)
  set_value(name, serializer.serialized_data)
end
resource(name, source_hash = nil, &block) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 50
def resource(name, source_hash = nil, &block)
  source_hash ||= @root_hash[name]
  set_value(name, serialize_resource(source_hash, &block))
end
resources(name, source_hashes = nil, &block) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 55
def resources(name, source_hashes = nil, &block)
  source_hashes ||= @root_hash[name]

  self.serialized_data[name] = (source_hashes || []).inject([]) do |result, source_hash|
    data = serialize_resource(source_hash, &block)
    data.empty? ? result : (result << data)
  end
end
serialize_collection(name, objects, klass, *args) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 19
def serialize_collection(name, objects, klass, *args)
  raise ArgumentError, "You should provide serializer klass" if !klass
  self.serialized_data[name] = []
  objects.each do |object|
   self.serialized_data[name] << klass.serialize(object, *args)
  end
end
serialize_entity(name, object, klass, *args) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 27
def serialize_entity(name, object, klass, *args)
  raise ArgumentError, "You should provide serializer klass" if !klass
  self.serialized_data[name] = klass.serialize(object, *args)
end

Protected Instance Methods

serialize_resource(source_hash, &block) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 66
def serialize_resource(source_hash, &block)
  return nil unless source_hash
  serializer = self.class.new(source_hash, @options)
  serializer.instance_exec(source_hash, &block)
  serializer.serialized_data
end
set_value(name, value) click to toggle source
# File lib/active_serializer/serializers/hash_serializer.rb, line 73
def set_value(name, value)
  self.serialized_data[name] = value
end