module LunaPark::Extensions::Serializable::InstanceMethods

Constants

HASHABLE
SERIALIZABLE

Public Instance Methods

inspect() click to toggle source
# File lib/luna_park/extensions/serializable.rb, line 73
def inspect
  attrs = self.class.serializable_attributes_list.map do |attr|
    value = instance_variable_get(:"@#{attr}")
    "#{attr}=#{value.inspect}" if value
  end
  "#<#{self.class.name} #{attrs.compact.join(' ')}>"
end
serialize() click to toggle source

Serialize object using methods, described with `::comparable_attributes` method

# File lib/luna_park/extensions/serializable.rb, line 59
def serialize
  self.class
      .serializable_attributes_list
      .each_with_object({}) do |field, output|
    next unless instance_variable_defined?(:"@#{field}")

    output[field] = serialize_value__(send(field))
  end
end
Also aliased as: to_h
to_h()

For powerfull polymorphism with Hashes

Alias for: serialize

Private Instance Methods

serialize_value__(value) click to toggle source
# File lib/luna_park/extensions/serializable.rb, line 86
def serialize_value__(value) # rubocop:disable Metrics/CyclomaticComplexity
  case value
  when Array then value.map              { |v| serialize_value__(v) } # TODO: work with Array (wrap values)
  when Hash  then value.transform_values { |v| serialize_value__(v) }
  when nil   then nil
  when SERIALIZABLE then value.serialize
  when HASHABLE     then value.to_h
  else value
  end
end