Class: DataImport::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/data-import/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(plan) ⇒ Runner

Returns a new instance of Runner.



4
5
6
7
8
9
10
11
# File 'lib/data-import/runner.rb', line 4

def initialize(plan)
  @plan = plan
  @definitions = Hash[@plan.definitions.map do |definition|
                        [definition.name, definition]
                      end]

  @executed_definitions = []
end

Instance Method Details

#before_filterObject



38
39
40
# File 'lib/data-import/runner.rb', line 38

def before_filter
  @plan.before_filter
end

#definition(name) ⇒ Object



33
34
35
36
# File 'lib/data-import/runner.rb', line 33

def definition(name)
  raise "no definition found for '#{name}'" unless @definitions[name].present?
  @definitions[name]
end

#run(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/data-import/runner.rb', line 13

def run(options = {})
  definitions_to_execute = definitions_for_execution(options[:only])
  while @executed_definitions.count < definitions_to_execute.count
    did_execute = false
    definitions_to_execute.each do |name|
      candidate = definition(name)
      next if @executed_definitions.include?(name)
      if (candidate.dependencies - @executed_definitions).blank?
        candidate.run(self)
        @executed_definitions << name
        did_execute = true
      end
    end

    unless did_execute
      raise "something went wrong! Could not execute all necessary definitions: #{candidate.dependencies - @@executed_definitions}"
    end
  end
end