Class: Synced::Engine::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/synced/engine/synchronizer.rb

Overview

Synchronizer class which performs actual synchronization between local database and given array of remote objects

Defined Under Namespace

Classes: MissingAPIClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_objects, model_class, options = {}) ⇒ Synchronizer

Initializes a new Synchronizer

Parameters:

  • remote_objects (Array|NilClass)

    Array of objects to be synchronized with local database. Objects need to respond to at least :id message. If it’s nil, then synchronizer will fetch the remote objects on it’s own.

  • model_class (Class)

    ActiveRecord model class from which local objects will be created.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • scope: (Symbol)

    Within this object scope local objects will be synchronized. By default it’s model_class.

  • id_key: (Symbol)

    attribute name under which remote object’s ID is stored, default is :synced_id.

  • synced_all_at_key: (Symbol)

    attribute name under which remote object’s sync time is stored, default is :synced_all_at

  • data_key: (Symbol)

    attribute name under which remote object’s data is stored.

  • local_attributes: (Array)

    Array of attributes in the remote object which will be mapped to local object attributes.

  • remove: (Boolean)

    If it’s true all local objects within current scope which are not present in the remote array will be destroyed. If only_updated is enabled, ids of objects to be deleted will be taken from the meta part. By default if cancel_at column is present, all missing local objects will be canceled with cancel_all, if it’s missing, all will be destroyed with destroy_all. You can also force method to remove local objects by passing it to remove: :mark_as_missing.

  • only_updated: (Boolean)

    If true requests to API will take advantage of updated_since param and fetch only created/changed/deleted remote objects



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/synced/engine/synchronizer.rb', line 35

def initialize(remote_objects, model_class, options = {})
  @model_class       = model_class
  @scope             = options[:scope]
  @id_key            = options[:id_key]
  @synced_all_at_key = options[:synced_all_at_key]
  @data_key          = options[:data_key]
  @remove            = options[:remove]
  @only_updated      = options[:only_updated]
  @local_attributes  = Array(options[:local_attributes])
  @associations      = Array(options[:associations])
  @remote_objects    = Array(remote_objects) if remote_objects
  @request_performed = false
end

Instance Attribute Details

#id_keyObject (readonly)

Returns the value of attribute id_key.



4
5
6
# File 'lib/synced/engine/synchronizer.rb', line 4

def id_key
  @id_key
end

Instance Method Details

#performObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/synced/engine/synchronizer.rb', line 49

def perform
  relation_scope.transaction do
    remove_relation.send(remove_strategy) if @remove

    remote_objects.map do |remote|
      local_object = local_object_by_remote_id(remote.id) || relation_scope.new
      local_object.attributes = default_attributes_mapping(remote)
      local_object.attributes = local_attributes_mapping(remote)
      local_object.save! if local_object.changed?
      local_object.tap do |local_object|
        @associations.each do |association|
          klass = association.to_s.classify.constantize
          klass.synchronize(remote: remote[association], scope: local_object,
            remove: @remove)
        end
      end
    end.tap do |local_objects|
      if updated_since_enabled? && @request_performed
        relation_scope.update_all(@synced_all_at_key => Time.now)
      end
    end
  end
end