Class: Friendly::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly/translator.rb

Constant Summary collapse

RESERVED_ATTRS =
[:id, :created_at, :updated_at].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer = Rufus::Json, time = Time) ⇒ Translator

Returns a new instance of Translator.



9
10
11
12
# File 'lib/friendly/translator.rb', line 9

def initialize(serializer = Rufus::Json, time = Time)
  @serializer = serializer
  @time       = time
end

Instance Attribute Details

#serializerObject (readonly)

Returns the value of attribute serializer.



7
8
9
# File 'lib/friendly/translator.rb', line 7

def serializer
  @serializer
end

#timeObject (readonly)

Returns the value of attribute time.



7
8
9
# File 'lib/friendly/translator.rb', line 7

def time
  @time
end

Instance Method Details

#to_object(klass, record) ⇒ Object



14
15
16
17
18
19
# File 'lib/friendly/translator.rb', line 14

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

#to_record(document) ⇒ Object



21
22
23
24
25
26
# File 'lib/friendly/translator.rb', line 21

def to_record(document)
  { :id         => document.id,
    :created_at => document.created_at,
    :updated_at => time.new,
    :attributes => serialize(document) }
end