Class: Fire::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/fire/runner.rb

Overview

Runner class takes a rule system and runs it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
# File 'lib/fire/runner.rb', line 9

def initialize(system)
  @system = system
  @_post = []
end

Instance Attribute Details

#systemObject (readonly)

Returns the value of attribute system.



15
16
17
# File 'lib/fire/runner.rb', line 15

def system
  @system
end

Instance Method Details

#post(pre = nil) ⇒ Array<Symbol> (private)

List of prerequistes that have already been run. Keeping this list prevents the same prequistes from ever being run twice in the same session.

Returns:

  • (Array<Symbol>)

    Returns



69
70
71
72
# File 'lib/fire/runner.rb', line 69

def post(pre=nil)
  @_post.concat(pre) if pre
  @_post
end

#prepare(rule) ⇒ Object (private)

Execute rule by first running any outstanding prerequistes then then the rul procedure itself.



34
35
36
37
38
39
40
41
42
# File 'lib/fire/runner.rb', line 34

def prepare(rule)
  pre = resolve(rule)
  pre = pre - post
  pre = pre - [rule.name.to_sym] if rule.name
  pre.each do |r|
    r.call
  end
  post(pre)
end

#resolve(rule, todo = []) ⇒ Array<Symbol> (private)

Resolve prerequistes.

Returns:

  • (Array<Symbol>)

    Returns



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fire/runner.rb', line 51

def resolve(rule, todo=[])
  return [] if (rule.todo - todo).empty?
  left = rule.todo - todo
  list = left
  todo.concat(left)
  left.each do |r|
    t = system.tasks[r.to_sym]
    x = resolve(t, todo)
    list.concat(x)
  end
  list.uniq 
end

#run_rulesObject



18
19
20
21
22
# File 'lib/fire/runner.rb', line 18

def run_rules
  system.rules.each do |rule|
    rule.apply{ prepare(rule) }
  end
end

#run_task(trigger) ⇒ Object



25
26
27
28
# File 'lib/fire/runner.rb', line 25

def run_task(trigger)
  task = system.tasks[trigger.to_sym]
  task.apply{ prepare(task) }
end