Class: Restforce::DB::Runner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/restforce/db/runner.rb

Overview

Restforce::DB::Runner provides an abstraction for lookup timing during the synchronization process. It provides methods for accessing only recently- modified records within the context of a specific Mapping.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delay = 0, last_run_time = DB.last_run) ⇒ Runner

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

delay - A Numeric offet to apply to all record lookups. Can be

used to mitigate server timing issues.

last_run_time - A Time indicating the point at which new runs should

begin.


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

def initialize(delay = 0, last_run_time = DB.last_run)
  @delay = delay
  @last_run = last_run_time
  @record_cache = RecordCache.new
  @timestamp_cache = TimestampCache.new
end

Instance Attribute Details

#afterObject

Returns the value of attribute after.



11
12
13
# File 'lib/restforce/db/runner.rb', line 11

def after
  @after
end

#beforeObject

Returns the value of attribute before.



11
12
13
# File 'lib/restforce/db/runner.rb', line 11

def before
  @before
end

#last_runObject (readonly)

Returns the value of attribute last_run.



10
11
12
# File 'lib/restforce/db/runner.rb', line 10

def last_run
  @last_run
end

Instance Method Details

#database_instances(cached = true) ⇒ Object

Public: Iterate through recently-updated records for the database model record type defined by the current mapping.

cached - A Boolean reflecting whether or not the collection should be

fetched through this runner's RecordCache.

Returns an Enumerator yielding Restforce::DB::Instances::ActiveRecords.



86
87
88
89
90
91
92
# File 'lib/restforce/db/runner.rb', line 86

def database_instances(cached = true)
  if cached
    @record_cache.collection(@mapping, :database_record_type, options)
  else
    @mapping.database_record_type.all(options)
  end
end

#run(mapping) ⇒ Object

Public: Grant access to recently-updated records for a specific mapping.

mapping - A Restforce::DB::Mapping instance.

Yields self, in the context of the passed mapping. Returns nothing.



57
58
59
60
61
62
# File 'lib/restforce/db/runner.rb', line 57

def run(mapping)
  @mapping = mapping
  yield self
ensure
  @mapping = nil
end

#salesforce_instances(cached = true) ⇒ Object

Public: Iterate through recently-updated records for the Salesforce record type defined by the current mapping.

cached - A Boolean reflecting whether or not the collection should be

fetched through this runner's RecordCache.

Returns an Enumerator yielding Restforce::DB::Instances::Salesforces.



71
72
73
74
75
76
77
# File 'lib/restforce/db/runner.rb', line 71

def salesforce_instances(cached = true)
  if cached
    @record_cache.collection(@mapping, :salesforce_record_type, options)
  else
    @mapping.salesforce_record_type.all(options)
  end
end

#tick!Object

Public: Indicate that a new phase of the run is beginning. Updates the before/after timestamp to ensure that new lookups are properly filtered.

Returns the new run Time.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/restforce/db/runner.rb', line 39

def tick!
  @record_cache.reset
  @timestamp_cache.reset

  run_time = Time.now

  @before = run_time - @delay
  @after = last_run - @delay if @last_run

  @last_run = run_time
end