Class: Synchronisable::Worker Private

Inherits:
Object
  • Object
show all
Defined in:
lib/synchronisable/worker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for model synchronization.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(model, data, options) ⇒ Synchronisable::Context .run(model, data) ⇒ Synchronisable::Context .run(model) ⇒ Synchronisable::Context

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance of worker and initiates model synchronization.

Overloads:

  • .run(model, data, options) ⇒ Synchronisable::Context

    Parameters:

    • model (Class)

      model class to be synchronized

    • options (Hash)

      synchronization options

    Options Hash (options):

    • :include (Hash)

      assocations to be synchronized. Use this option to override has_one & has_many assocations defined in model synchronizer.

Returns:



27
28
29
30
31
32
# File 'lib/synchronisable/worker.rb', line 27

def run(model, *args)
  options = args.extract_options!
  data = args.first

  new(model, options).run(data)
end

Instance Method Details

#run(data) ⇒ Synchronisable::Context

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initiates model synchronization.

Parameters:

  • data (Array<Hash>)

    array of hashes with remote attriutes. If not specified worker will try to get the data using defined gateway class or fetch lambda/proc defined in corresponding synchronizer

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/synchronisable/worker.rb', line 43

def run(data)
  sync do |context|
    error_handler = ErrorHandler.new(@logger, context)
    context.before = @model.imports_count

    hashes = DataBuilder.build(@model, @synchronizer, data)
    hashes.each do |attrs|
      source = Source.new(@model, @parent, attrs)
      error_handler.handle(source) do
        @synchronizer.with_sync_callbacks(source) do
          sync_record(source)
          sync_associations(source)
          set_record_foreign_keys(source)
        end
      end
    end

    context.after = @model.imports_count
    context.deleted = 0
  end
end