Sha256: 9007eaca8ebc77298a44b64d095822b1aaed284b42449b01bba7a73ba3958588
Contents?: true
Size: 1.23 KB
Versions: 2
Compression:
Stored size: 1.23 KB
Contents
module OptionsModel module Concerns module Serialization extend ActiveSupport::Concern def to_h hash = {} hash.merge! unused_attributes if self.class.with_unused_attributes? hash.merge! attributes hash.merge! nested_attributes.reduce({}) { |h, (k, v)| h[k] = v.to_h; h } hash end module ClassMethods def dump(obj) return YAML.dump({}) unless obj unless obj.is_a? self raise ArgumentError, "can't dump: was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}" end YAML.dump obj.to_h end def load(yaml) return new unless yaml return new unless yaml.is_a?(String) && /^---/.match?(yaml) hash = YAML.load(yaml) || Hash.new unless hash.is_a? Hash raise ArgumentError, "can't load: was supposed to be a #{Hash}, but was a #{hash.class}. -- #{hash.inspect}" end new hash end def with_unused_attributes! @with_unused_attributes = true end def with_unused_attributes? @with_unused_attributes end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
options_model-0.0.5 | lib/options_model/concerns/serialization.rb |
options_model-0.0.4 | lib/options_model/concerns/serialization.rb |