Module: Disposable::Twin::Struct
- Defined in:
- lib/disposable/twin/struct.rb
Overview
Instance Method Summary collapse
- #hash_representer ⇒ Object
- #setup_properties!(model, options = {}) ⇒ Object
- #sync(options = {}) ⇒ Object (also: #sync!)
- #sync_hash_representer ⇒ Object
Instance Method Details
#hash_representer ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/disposable/twin/struct.rb', line 11 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 |
#setup_properties!(model, options = {}) ⇒ Object
7 8 9 |
# File 'lib/disposable/twin/struct.rb', line 7 def setup_properties!(model, ={}) hash_representer.new(self).from_hash(model.merge()) end |
#sync(options = {}) ⇒ Object Also known as: sync!
37 38 39 |
# File 'lib/disposable/twin/struct.rb', line 37 def sync(={}) sync_hash_representer.new(self).to_hash end |
#sync_hash_representer ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/disposable/twin/struct.rb', line 26 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 |