Class: Restforce::DB::Accumulator

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

Overview

Restforce::DB::Accumulator is responsible for the accumulation of changes over the course of a single synchronization run. As we iterate over the various mappings, we build a set of changes for each Salesforce ID, which is then applied to all objects synchronized with that Salesforce object.

Instance Method Summary collapse

Instance Method Details

#attributesObject

Public: Get the accumulated list of attributes after all changes have been applied.

Returns a Hash.



28
29
30
31
32
# File 'lib/restforce/db/accumulator.rb', line 28

def attributes
  @attributes ||= sort.reverse.inject({}) do |final, (_, changeset)|
    changeset.merge(final)
  end
end

#changed?(comparison) ⇒ Boolean

Public: Do the canonical attributes stored in this Accumulator differ from those in the passed comparison Hash?

comparison - A Hash mapping of attributes to values.

Returns a Boolean.

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/restforce/db/accumulator.rb', line 53

def changed?(comparison)
  attributes.any? do |attribute, value|
    next unless comparison.key?(attribute)
    comparison[attribute] != value
  end
end

#current(comparison) ⇒ Object

Public: Get a Hash representing the current values for the items in the passed Hash, as a subset of this Accumulator’s attributes Hash.

comparison - A Hash mapping of attributes to values.

Returns a Hash.



40
41
42
43
44
45
# File 'lib/restforce/db/accumulator.rb', line 40

def current(comparison)
  attributes.each_with_object({}) do |(attribute, value), final|
    next unless comparison.key?(attribute)
    final[attribute] = value
  end
end

#store(timestamp, changeset) ⇒ Object

Public: Store the changeset under the given timestamp. If a changeset for that timestamp has already been registered, merge it with the newly passed changeset.

timestamp - A Time object. changeset - A Hash mapping attribute names to values.

Returns nothing.



19
20
21
22
# File 'lib/restforce/db/accumulator.rb', line 19

def store(timestamp, changeset)
  return super unless key?(timestamp)
  self[timestamp].merge!(changeset)
end

#up_to_date_for?(timestamp) ⇒ Boolean

Public: Does the timestamp of the most recent change meet or exceed the specified timestamp?

timestamp - A Time object.

Returns a Boolean.

Returns:

  • (Boolean)


66
67
68
# File 'lib/restforce/db/accumulator.rb', line 66

def up_to_date_for?(timestamp)
  keys.sort.last >= timestamp
end