Sha256: adc18424af6776f66df16b339a41daed40513f0a473b0c3ce9c7f2d9fbe9b08e

Contents?: true

Size: 697 Bytes

Versions: 1

Compression:

Stored size: 697 Bytes

Contents

module Houston
  class ActiveRecordSerializer

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

    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

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

  end
end

Houston.add_serializer Houston::ActiveRecordSerializer.new

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
houston-core-0.8.4 lib/houston/boot/active_record_serializer.rb