Module: Disposable::Twin::Struct

Defined in:
lib/disposable/twin/struct.rb

Overview

Twin that uses a hash to populate.

Twin.new(id: 1)

Instance Method Summary collapse

Instance Method Details

#hash_representerObject



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, options={})
  hash_representer.new(self).from_hash(model.merge(options))
end

#sync(options = {}) ⇒ Object Also known as: sync!



37
38
39
# File 'lib/disposable/twin/struct.rb', line 37

def sync(options={})
  sync_hash_representer.new(self).to_hash
end

#sync_hash_representerObject



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