Module: ARMS::ActiveRecord::AttributeMethods::Serialization

Included in:
ActiveRecord::Base
Defined in:
lib/arms.rb

Instance Method Summary collapse

Instance Method Details

#arms_serialize(attr_name, *coders) ⇒ Object

ActiveRecord::Base.arms_serialize takes an attribute name and any number of coders which will be chained to serialize and deserialize between the database column and the model attribute.

full documentation is at MultiCoder#initialize.

here are a few example invocations:

# two coders: indifferent hashes, YAML with argument Hash (the object_class)
arms_serialize('preferences', :indifferent_hashes, [YAML, Hash])

# two coders: struct coder with argument Preference (the struct class), JSON coder
MultiCoder.new([[:struct, Preference], :json], attr_name: 'preferences', model: Foo)


81
82
83
84
# File 'lib/arms.rb', line 81

def arms_serialize(attr_name, *coders)
  multi_coder = ARMS::MultiCoder.new(coders, attr_name: attr_name, model: self)
  serialize(attr_name, multi_coder)
end