Sha256: 08793f8126debb30e154a290b28e5561127d133ab6b1718945d2a2df828ef63e
Contents?: true
Size: 897 Bytes
Versions: 8
Compression:
Stored size: 897 Bytes
Contents
require "rufus-json" module Friendly class Translator RESERVED_ATTRS = [:id, :created_at, :updated_at].freeze attr_reader :serializer, :time def initialize(serializer = Rufus::Json, time = Time) @serializer = serializer @time = time end def to_object(klass, record) record.delete(:added_id) attributes = serializer.decode(record.delete(:attributes)) attributes.merge!(record).merge!(:new_record => false) klass.new_without_change_tracking attributes end def to_record(document) { :id => document.id, :created_at => document.created_at, :updated_at => time.new, :attributes => serialize(document) } end protected def serialize(document) attrs = document.to_hash.reject { |k,v| RESERVED_ATTRS.include?(k) } serializer.encode(attrs) end end end
Version data entries
8 entries across 8 versions & 2 rubygems