class Restforce::DB::Collector

Restforce::DB::Collector is responsible for grabbing the attributes from recently-updated records for purposes of synchronization. It relies on the mappings configured in instances of Restforce::DB::RecordTypes::Base to locate recently-updated records and fetch their attributes.

Public Instance Methods

run(accumulator = nil) click to toggle source

Public: Run the collection process, pulling in records from Salesforce and the database to determine the lists of attributes to apply to all mapped records.

accumulator - A Hash-like accumulator object.

Returns a Hash mapping Salesforce ID/type combinations to accumulators.

# File lib/restforce/db/collector.rb, line 18
def run(accumulator = nil)
  @accumulated_changes = accumulator || accumulated_changes

  @runner.run(@mapping) do |run|
    run.salesforce_instances.each { |instance| accumulate(instance) }
    run.database_instances.each { |instance| accumulate(instance) }
  end

  accumulated_changes
ensure
  # Clear out the results of this run so we start fresh next time.
  @accumulated_changes = nil
end

Private Instance Methods

accumulate(instance) click to toggle source

Internal: Append the passed instance’s attributes to its accumulated list of changesets.

instance - A Restforce::DB::Instances::Base.

Returns nothing.

# File lib/restforce/db/collector.rb, line 47
def accumulate(instance)
  return unless instance.synced? && @runner.changed?(instance)

  accumulated_changes[key_for(instance)].store(
    instance.last_update,
    instance.attributes,
  )
end
accumulated_changes() click to toggle source

Internal: Get a Hash to collect accumulated changes.

Returns a Hash of Hashes.

# File lib/restforce/db/collector.rb, line 37
def accumulated_changes
  @accumulated_changes ||= Hash.new { |h, k| h[k] = {} }
end
key_for(instance) click to toggle source

Internal: Get a unique key with enough information to look up the passed instance in Salesforce.

instance - A Restforce::DB::Instances::Base.

Returns an Object.

# File lib/restforce/db/collector.rb, line 62
def key_for(instance)
  [instance.id, instance.mapping.salesforce_model]
end