module AMF module Serialization class AMFSerializer < ActiveRecord::Serialization::Serializer def initialize(record, options = {}) super(record, options) @options[:amf_version] = 3 end def serialize if @options[:amf_version] == 3 serializer_class = AMF::AMF3Serializer elsif @options[:amf_version] == 0 serializer_class = AMF::AMF0Serializer else raise AMF::AMFError, "unsupported serializer version #{@options[:amf_version]}" end serializer = serializer_class.new({:serializable_names => serializable_names, :options => @options}) serializer.serialize(@record) do |s| add_includes do |association, records, opts| add_associations(association, records, opts, s) end yield serializer if block_given? end end def add_associations(association, records, opts, serializer) serializer.write_utf8_vr(association.to_s) if records.is_a?(Enumerable) serializer.stream << records.each { |record| record.to_amf(opts) }.join("") else if record = @record.send(association) serializer.stream << record.to_amf(opts) end end end end end module ActiveRecord def self.included(base) base.send :include, InstanceMethods end module InstanceMethods def to_amf(options = {}, &block) serializer = AMF::Serialization::AMFSerializer.new(self, options) block_given? ? serializer.to_s(&block) : serializer.to_s end def from_amf(amf, options = {}) options[:amf_version] ||= 3 self.attributes = AMF.deserialize(amf, options[:amf_version]) self end end end end