Class: DataMapper::Is::Serialized::Filters::ModelSerializeFilter

Inherits:
SerializeFilter
  • Object
show all
Defined in:
lib/dm-is-serialized/is/filters.rb

Overview

serialize: :product => product_id

Instance Attribute Summary

Attributes inherited from SerializeFilter

#name, #record

Instance Method Summary collapse

Methods inherited from SerializeFilter

#property, #relationship

Instance Method Details

#deserialize(id) ⇒ Object

product_id => id



41
42
43
44
# File 'lib/dm-is-serialized/is/filters.rb', line 41

def deserialize(id)
  ids = id.split("|")
  model.get(*ids)
end

#modelObject



46
47
48
# File 'lib/dm-is-serialized/is/filters.rb', line 46

def model
  Object.const_get(@name.to_s.camel_case)
end

#serialize(record) ⇒ Object

serialize(:product)



33
34
35
36
37
38
# File 'lib/dm-is-serialized/is/filters.rb', line 33

def serialize(record)
  keys = model.key.map { |property| property.name }
  raise "Record can't be nil (#{model} with keys #{keys})" if record.nil?
  values = keys.map { |key| record.send(key) }
  values.length.eql?(1) ? values.first : values.join("|")
end