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.

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:



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

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

  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:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/synchronisable/controller.rb', line 53

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