Class: Synchronisable::Controller Private

Inherits:
Object
  • Object
show all
Includes:
Helper::Logging
Defined in:
lib/synchronisable/controller.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.

Constant Summary collapse

VALID_OPTIONS =

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

%i(includes parent)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

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.



19
20
21
# File 'lib/synchronisable/controller.rb', line 19

def logger
  @logger
end

Class Method Details

.call(model, data, options) ⇒ Synchronisable::Context .call(model, data) ⇒ Synchronisable::Context .call(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 controller and initiates model synchronization.

Overloads:

  • .call(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:



36
37
38
39
# File 'lib/synchronisable/controller.rb', line 36

def call(model, *args)
  data, options = *extract_args(args)
  new(model, options).call(data)
end

Instance Method Details

#call(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 (Hash, Array<Hash>, Array<String>, Array<Integer>, String, Integer)

    synchronization data. If not specified, it will try to get array of hashes to sync with using defined gateway class or ‘fetch` lambda/proc defined in corresponding synchronizer

Returns:

See Also:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/synchronisable/controller.rb', line 78

def call(data)
  sync do |context|
    error_handler = ErrorHandler.new(logger, context)
    source = Source.new(@model, @parent, @includes)

    context.before = @model.imports_count

    hashes = @input.parse(data)

    hashes.each do |attrs|
      error_handler.handle(source) do
        source.prepare(data, attrs)

        record_worker = Worker::Record.new(@synchronizer, source)
        associations_worker = Worker::Associations.new(@synchronizer, source)

        @synchronizer.with_sync_callbacks(source) do
          associations_worker.sync_parent_associations
          record_worker.sync_record
          associations_worker.sync_child_associations
        end
      end
    end

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