Class: Friendly::Translator

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

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

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

- (Object) serializer (readonly)

Returns the value of attribute serializer



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

def serializer
  @serializer
end

- (Object) time (readonly)

Returns the value of attribute time



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

def time
  @time
end

Instance Method Details

- (Object) to_object(klass, record)



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

- (Object) to_record(document)



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