Class: Friendly::Translator
- Inherits:
-
Object
- Object
- Friendly::Translator
- Defined in:
- lib/friendly/translator.rb
Constant Summary collapse
- RESERVED_ATTRS =
[:id, :created_at, :updated_at].freeze
Instance Attribute Summary collapse
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
-
#initialize(serializer = Rufus::Json, time = Time) ⇒ Translator
constructor
A new instance of Translator.
- #to_object(klass, record) ⇒ Object
- #to_record(document) ⇒ Object
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
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
7 8 9 |
# File 'lib/friendly/translator.rb', line 7 def serializer @serializer end |
#time ⇒ Object (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 |