Class: Restforce::DB::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/restforce/db/synchronizer.rb

Overview

Restforce::DB::Synchronizer is responsible for synchronizing the records in Salesforce with the records in the database. It relies on the mappings configured in instances of Restforce::DB::RecordTypes::Base to create and update records with the appropriate values.

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ Synchronizer

Public: Initialize a new Restforce::DB::Synchronizer.

mapping - A Restforce::DB::Mapping.



14
15
16
# File 'lib/restforce/db/synchronizer.rb', line 14

def initialize(mapping)
  @mapping = mapping
end

Instance Method Details

#run(changes) ⇒ Object

Public: Synchronize records for the current mapping from a Hash of record descriptors to attributes.

NOTE: Synchronizer assumes that the propagation step has done its job correctly. If we can’t locate a database record for a specific Salsforce ID, we assume it shouldn’t be synchronized.

changes - A Hash, with keys composed of a Salesforce ID and model name,

with Restforce::DB::Accumulator objects as values.

Returns nothing.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/restforce/db/synchronizer.rb', line 29

def run(changes)
  changes.each do |(id, salesforce_model), accumulator|
    next unless salesforce_model == @mapping.salesforce_model

    database_instance = @mapping.database_record_type.find(id)
    salesforce_instance = @mapping.salesforce_record_type.find(id)
    next unless database_instance && salesforce_instance

    most_recent_timestamp = [
      database_instance.last_update,
      salesforce_instance.last_update,
    ].max
    next unless accumulator.up_to_date_for?(most_recent_timestamp)

    update(database_instance, accumulator)
    update(salesforce_instance, accumulator)
  end
end