Module: Disposable::Twin::Sync

Includes:
SyncOptions, ToNestedHash, Writeable
Defined in:
lib/disposable/twin/sync.rb

Defined Under Namespace

Modules: SkipUnchanged, SyncOptions, ToNestedHash, Writeable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Writeable

#sync_options

Methods included from SyncOptions

#sync_options

Methods included from ToNestedHash

#to_nested_hash

Class Method Details

.included(includer) ⇒ Object



38
39
40
# File 'lib/disposable/twin/sync.rb', line 38

def self.included(includer)
  includer.extend ToNestedHash::ClassMethods
end

Instance Method Details

#sync!(options) ⇒ Object

Sync all scalar attributes, call sync! on nested and return model.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/disposable/twin/sync.rb', line 19

def sync!(options) # semi-public.
  options_for_sync = sync_options(Decorator::Options[options])

  schema.each(options_for_sync) do |dfn|
    unless dfn[:twin]
      mapper.send(dfn.setter, send(dfn.getter)) # always sync the property
      next
    end

    nested_model = PropertyProcessor.new(dfn, self).() { |twin| twin.sync!({}) }

    next if nested_model.nil?

    mapper.send(dfn.setter, nested_model) # @model.artist = <Artist>
  end

  model
end

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



8
9
10
11
12
# File 'lib/disposable/twin/sync.rb', line 8

def sync_models(options={})
  return yield to_nested_hash if block_given?

  sync!(options)
end