Sha256: 3bd1d2c8d8c72f92c456eee37b40e27b751a005622cbf819fb5ce1e43d167419
Contents?: true
Size: 1.24 KB
Versions: 2
Compression:
Stored size: 1.24 KB
Contents
module OptionsModel module Concerns module Serialization extend ActiveSupport::Concern def to_h hash = {} self.class.attribute_names.each do |attribute_name| attribute = public_send(attribute_name) if attribute.is_a?(OptionsModel::Base) hash[attribute_name] = attribute.to_h else hash[attribute_name] = attribute end end hash end def to_h_with_unused to_h.merge unused_attributes 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 end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
options_model-0.0.3 | lib/options_model/concerns/serialization.rb |
options_model-0.0.2 | lib/options_model/concerns/serialization.rb |