Module: Disposable::Twin::Sync
- Includes:
- SyncOptions, ToNestedHash, Writeable
- Defined in:
- lib/disposable/twin/sync.rb
Defined Under Namespace
Modules: SkipGetter, SkipUnchanged, SyncOptions, ToNestedHash, Writeable
Class Method Summary collapse
-
.hash_representer(twin_class, &block) ⇒ Object
Creates a fresh copy of the internal representer and adds Representable::Hash.
Instance Method Summary collapse
-
#sync!(options) ⇒ Object
Sync all scalar attributes, call sync! on nested and return model.
- #sync_models(options = {}) ⇒ Object (also: #sync)
Methods included from Writeable
Methods included from SyncOptions
Methods included from ToNestedHash
#nested_hash_source, #to_nested_hash
Class Method Details
.hash_representer(twin_class, &block) ⇒ Object
Creates a fresh copy of the internal representer and adds Representable::Hash. This is used wherever a hash transformation is needed.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/disposable/twin/sync.rb', line 10 def self.hash_representer(twin_class, &block) Schema.from(twin_class, recursive: false, representer_from: lambda { |twin_class| twin_class.representer_class }, superclass: Representable::Decorator, include: Representable::Hash, exclude_options: [:default], # TODO: TEST IN default_test. &block ) end |
Instance Method Details
#sync!(options) ⇒ Object
Sync all scalar attributes, call sync! on nested and return model.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/disposable/twin/sync.rb', line 32 def sync!() # semi-public. # TODO: merge this into Sync::Run or something and use in Struct, too, so we don't # need the representer anymore? = (Decorator::Options[]) schema.each() do |dfn| property_value = sync_read(dfn) # unless dfn[:twin] mapper.send(dfn.setter, property_value) # always sync the property next end nested_model = PropertyProcessor.new(dfn, self, property_value).() { |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
21 22 23 24 25 |
# File 'lib/disposable/twin/sync.rb', line 21 def sync_models(={}) return yield to_nested_hash if block_given? sync!() end |