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 = JSON, time = Time) ⇒ Translator

Returns a new instance of Translator.



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

def initialize(serializer = JSON, time = Time)
  @serializer = serializer
  @time       = time
end

Instance Attribute Details

#serializerObject (readonly)

Returns the value of attribute serializer.



5
6
7
# File 'lib/friendly/translator.rb', line 5

def serializer
  @serializer
end

#timeObject (readonly)

Returns the value of attribute time.



5
6
7
# File 'lib/friendly/translator.rb', line 5

def time
  @time
end

Instance Method Details

#to_object(klass, record) ⇒ Object



12
13
14
15
16
# File 'lib/friendly/translator.rb', line 12

def to_object(klass, record)
  record.delete(:added_id)
  attributes = serializer.parse(record.delete(:attributes))
  klass.new attributes.merge(record).merge(:new_record => false)
end

#to_record(document) ⇒ Object



18
19
20
21
22
23
# File 'lib/friendly/translator.rb', line 18

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