Class: Houston::ActiveRecordSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/houston/boot/serializers/active_record_serializer.rb

Instance Method Summary collapse

Instance Method Details

#applies_to?(object) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/houston/boot/serializers/active_record_serializer.rb', line 4

def applies_to?(object)
  object.is_a?(ActiveRecord::Base)
end

#pack(record) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/houston/boot/serializers/active_record_serializer.rb', line 8

def pack(record)
  model = record.class
  type_caster = model.type_caster
  normal_attributes = record.attributes.each_with_object({}) do |(attribute, value), attributes|
    attributes[attribute] = type_caster.type_cast_for_database(attribute, value)
  end
  { "class" => model.name, "attributes" => normal_attributes }
end

#unpack(object) ⇒ Object



17
18
19
20
# File 'lib/houston/boot/serializers/active_record_serializer.rb', line 17

def unpack(object)
  klass, attributes = object.values_at("class", "attributes")
  klass.constantize.instantiate(attributes)
end