Sha256: 6432e23a16dc51bcce86822389bf1dd455dd2c3bc3883d678a97abb963ac3218
Contents?: true
Size: 1.17 KB
Versions: 6
Compression:
Stored size: 1.17 KB
Contents
module Disposable class Twin # Twin that uses a hash to populate. # # Twin.new(id: 1) module Struct def setup_properties!(model, options={}) hash_representer.new(self).from_hash(model.merge(options)) end def hash_representer Class.new(schema) do include Representable::Hash include Representable::Hash::AllowSymbols representable_attrs.each do |dfn| dfn.merge!( prepare: lambda { |model, *| model }, instance: lambda { |model, *| model }, # FIXME: this is because Representable thinks this is typed? in Deserializer. representable: false, ) if dfn[:twin] end end end def sync_hash_representer hash_representer.clone.tap do |rpr| rpr.representable_attrs.each do |dfn| dfn.merge!( serialize: lambda { |model, *| model.sync! }, representable: true ) if dfn[:twin] end end end def sync(options={}) sync_hash_representer.new(self).to_hash end alias_method :sync!, :sync end end end
Version data entries
6 entries across 6 versions & 1 rubygems