class ActiveRecord::MessagePack::Encoder

def add_cached_associations(record, entry)

def add_cached_associations(record, entry)
  record.class.reflections.each_value do |reflection|
    if record.association_cached?(reflection.name)
      entry << reflection.name << encode(record.association(reflection.name).target)
    end
  end
end

def build_entry(record)

def build_entry(record)
  [
    ActiveSupport::MessagePack::Extensions.dump_class(record.class),
    record.attributes_for_database,
    record.new_record?
  ]
end

def encode(input)

def encode(input)
  if input.is_a?(Array)
    input.map { |record| encode_record(record) }
  elsif input
    encode_record(input)
  end
end

def encode_record(record)

def encode_record(record)
  ref = @refs[record]
  if !ref
    ref = @refs[record] = @entries.size
    @entries << build_entry(record)
    add_cached_associations(record, @entries.last)
  end
  ref
end

def initialize

def initialize
  @entries = []
  @refs = {}.compare_by_identity
end