module Disposable::Twin::Sync

Public Class Methods

hash_representer(twin_class, &block) click to toggle source

Creates a fresh copy of the internal representer and adds Representable::Hash. This is used wherever a hash transformation is needed.

# File lib/disposable/twin/sync.rb, line 22
def self.hash_representer(twin_class, &block)
  Disposable::Rescheme.from(twin_class,
    recursive: false,
    definitions_from: lambda { |twin_klass| twin_klass.definitions },
    superclass: Representable::Decorator,
    include: Representable::Hash,
    exclude_options: [:default], # TODO: TEST IN default_test.
    &block
  )
end

Private Class Methods

included(includer) click to toggle source
# File lib/disposable/twin/sync.rb, line 66
def self.included(includer)
  includer.extend ToNestedHash::ClassMethods
end

Public Instance Methods

sync(options={})
Alias for: sync_models
sync!(options) click to toggle source

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

# File lib/disposable/twin/sync.rb, line 41
def sync!(options) # semi-public.
  # TODO: merge this into Sync::Run or something and use in Struct, too, so we don't
  # need the representer anymore?
  options_for_sync = sync_options(Options[options])

  schema.each(options_for_sync) do |dfn|
    property_value = sync_read(dfn) #

    unless dfn[:nested]
      mapper.send(dfn.setter, property_value) # always sync the property
      next
    end

    # First, call sync! on nested model(s).
    nested_model = PropertyProcessor.new(dfn, self, property_value).() { |twin| twin.sync!({}) }
    next if nested_model.nil?

    # Then, write nested model to parent model, e.g. model.songs = [<Song>]
    mapper.send(dfn.setter, nested_model) # @model.artist = <Artist>
  end

  model
end
sync_models(options={}) { |to_nested_hash| ... } click to toggle source
# File lib/disposable/twin/sync.rb, line 33
def sync_models(options={})
  return yield to_nested_hash if block_given?

  sync!(options)
end
Also aliased as: sync

Private Instance Methods

sync_read(definition) click to toggle source
# File lib/disposable/twin/sync.rb, line 70
def sync_read(definition)
  send(definition.getter)
end