Class: Sync

Inherits:
Object
  • Object
show all
Includes:
Interpipe::Interactor
Defined in:
app/interactors/sync.rb

Overview

An interactor class for inserting or updating model instances to the database.

In order to use the this interactor, you first need to create a configured descendant of Sync by calling Sync.[] operator.

Examples:

configure and use the Sync interactor

sync = Sync[
  Event,
  matching_attributes: [:faculty, :source_type, :source_id, :absolute_sequence_number],
  skip_updating: [:relative_sequence_number]
]
sync.perform(events: [event1, event2])

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize, #setup

Class Attribute Details

.key_nameObject

Returns the value of attribute key_name


22
23
24
# File 'app/interactors/sync.rb', line 22

def key_name
  @key_name
end

.matching_attributesObject

Returns the value of attribute matching_attributes


22
23
24
# File 'app/interactors/sync.rb', line 22

def matching_attributes
  @matching_attributes
end

.model_classObject

Returns the value of attribute model_class


22
23
24
# File 'app/interactors/sync.rb', line 22

def model_class
  @model_class
end

.skip_updatingObject

Returns the value of attribute skip_updating


22
23
24
# File 'app/interactors/sync.rb', line 22

def skip_updating
  @skip_updating
end

Class Method Details

.[](model_class, key_name: nil, matching_attributes: [:id], skip_updating: []) ⇒ Class

Creates a new Sync descendant class with parameters set for syncing a specific model. In order to properly use Sync functionality, you need to use this operator.

Parameters:

  • model_class (Class)

    Class of the model that should be synced (should inherit from Sequel::Model).

  • key_name (Symbol) (defaults to: nil)

    Optional name of the key under which the collection of models for syncing is stored in #perform args hash. Defaults to inferred table name from model_class name.

  • matching_attributes (Array<Symbol>) (defaults to: [:id])

    List of columns that are used for matching model instances with existing database records. These columns can't be updated.

  • skip_updating (Array<Symbol>) (defaults to: [])

    List of columns that should not be updated in case the model already exists in the database. If the model is newly inserted, this configuration is ignored.

Returns:

  • (Class)

    a new class which inherits from Sync and is configured for syncing instances of a specific model


38
39
40
41
42
43
44
45
# File 'app/interactors/sync.rb', line 38

def [](model_class, key_name: nil, matching_attributes: [:id], skip_updating: [])
  cls = Class.new(self)
  cls.model_class = model_class
  cls.key_name = generate_key_name(model_class, key_name)
  cls.matching_attributes = matching_attributes
  cls.skip_updating = skip_updating
  cls
end

.generate_key_name(model_class, key_name) ⇒ Object


47
48
49
50
# File 'app/interactors/sync.rb', line 47

def generate_key_name(model_class, key_name)
  return key_name if key_name
  model_class.to_s.tableize.to_sym
end

Instance Method Details

#perform(args) ⇒ Object

Synchronizes a collection of models.

Parameters:

  • args (Hash)

    with collection of models under key configured by key_name parameter


56
57
58
59
60
61
62
63
# File 'app/interactors/sync.rb', line 56

def perform(args)
  @models = args[key_name]
  raise "Missing key #{key_name} in Sync#perform arguments." unless @models
  @rest = args.select { |k,v| k != key_name }
  @models.each do |model|
    upsert_model(model)
  end
end

#resultsObject


65
66
67
# File 'app/interactors/sync.rb', line 65

def results
  {key_name => @models}.merge(@rest)
end