Module: StoreConfigurable::Object::Coding
- Included in:
- StoreConfigurable::Object
- Defined in:
- lib/store_configurable/object.rb
Overview
Class methods so the StoreConfigurable::Object
responds to dump
and load
which allows it to conform to ActiveRecord’s coder requirement via its serialize method that we use.
The dump
method serializes the raw data behind the StoreConfigurable::Object
proxy object. This means that we only store pure ruby primitives in the datbase, not our proxy object’s YAML type.
The load
method mimics ActiveRecord::Coders::YAMLColumn
internals by retuning a new object when needed as well as making sure that the YAML we are process if of the same type. When reconstituting a StoreConfigurable::Object
we must set the store’s owner as this does. That way as our recursive lambda loader regenerates the tree of config data, we always have a handle for each DirtyTrackingOrderedOptions
object to report state changes back to the owner. Finally, after each load we make sure to clear out changes so reloaded objects are not marked as dirty.
Instance Method Summary collapse
Instance Method Details
#dump(value) ⇒ Object
39 40 41 42 |
# File 'lib/store_configurable/object.rb', line 39 def dump(value) config = (value.nil? ? StoreConfigurable::Object.new : value).__config__ YAML.dump OMAP_CONVERTER.call(config) end |
#load(yaml, owner = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/store_configurable/object.rb', line 44 def load(yaml, owner=nil) return StoreConfigurable::Object.new if yaml.blank? return yaml unless yaml.is_a?(String) && yaml =~ /^---/ stored_data = YAML.load(yaml) unless stored_data.is_a?(Hash) raise ActiveRecord::SerializationTypeMismatch, "Attribute was supposed to be a Hash, but was a #{stored_data.class}" end config = StoreConfigurable::Object.new config.__store_configurable_owner__ = owner stored_data.each { |k,v| YAML_LOADER.call(config, k, v) } owner.changed_attributes.delete('_config') if owner config end |