Class: Restforce::DB::Synchronizer
- 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
-
#run(changes) ⇒ Object
Public: Synchronize records for the current mapping from a Hash of record descriptors to attributes.
-
#update(instance, accumulator) ⇒ Object
Public: Update the passed instance with the accumulated attributes from a synchronization run.
Methods inherited from Task
Constructor Details
This class inherits a constructor from Restforce::DB::Task
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 Salesforce 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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/restforce/db/synchronizer.rb', line 22 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) next unless database_instance && up_to_date?(database_instance, accumulator) salesforce_instance = @mapping.salesforce_record_type.find(id) next unless salesforce_instance && up_to_date?(salesforce_instance, accumulator) update(database_instance, accumulator) update(salesforce_instance, accumulator) end end |
#update(instance, accumulator) ⇒ Object
Public: Update the passed instance with the accumulated attributes from a synchronization run.
instance - An instance of Restforce::DB::Instances::Base. accumulator - A Restforce::DB::Accumulator.
Returns nothing.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/restforce/db/synchronizer.rb', line 44 def update(instance, accumulator) return unless accumulator.changed?(instance.attributes) current_attributes = accumulator.current(instance.attributes) attributes = @mapping.convert(instance.record_type, current_attributes) instance.update!(attributes) @runner. instance rescue ActiveRecord::ActiveRecordError, Faraday::Error::ClientError => e DB.logger.error(SynchronizationError.new(e, instance)) end |