Class: UltraMarathon::AbstractRunner

Inherits:
Object
  • Object
show all
Includes:
Callbacks, Instrumentation, Logging
Defined in:
lib/ultra_marathon/abstract_runner.rb

Constant Summary

Constants included from Instrumentation

Instrumentation::TIME_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#instrumentations

Methods included from Logging

#logger

Instance Attribute Details

#successObject

Returns the value of attribute success.



12
13
14
# File 'lib/ultra_marathon/abstract_runner.rb', line 12

def success
  @success
end

Class Method Details

.run(name = :main, options = {}, &block) ⇒ Object

This is where the magic happens. Called in the class context, it will be safely executed in the context of the instance.

E.g.

class BubblesRunner < AbstractRunner

run do
  fire_the_missiles
  take_a_nap
end

def fire_the_missiles
  puts 'But I am le tired'
end

def take_a_nap
  puts 'zzzzzz'
end

end

BubblesRunner.new.run!
# => 'But I am le tired'
# => 'zzzzzz'


81
82
83
84
85
86
87
88
89
# File 'lib/ultra_marathon/abstract_runner.rb', line 81

def run(name=:main, options={}, &block)
  name = name.to_sym
  if !run_blocks.key? name
    options[:name] = name
    self.run_blocks[name] = [options, block]
  else
    raise NameError.new("Run block named #{name} already exists!")
  end
end

.run_blocksObject



91
92
93
# File 'lib/ultra_marathon/abstract_runner.rb', line 91

def run_blocks
  @run_blocks ||= Hash.new
end

Instance Method Details

#resetObject

Resets success to being true, unsets the failed sub_runners to [], and sets the unrun sub_runners to be the uncompleted/failed ones



44
45
46
47
48
49
# File 'lib/ultra_marathon/abstract_runner.rb', line 44

def reset
  reset_failed_runners
  @success = true
  invoke_on_reset_callbacks
  self
end

#run!Object

Runs the run block safely in the context of the instance



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ultra_marathon/abstract_runner.rb', line 22

def run!
  if self.class.run_blocks.any?
    begin
      self.success = true
      invoke_before_run_callbacks
      instrument(:run_unrun_sub_runners) { run_unrun_sub_runners }
      self.success = failed_sub_runners.empty?
    rescue StandardError => error
      invoke_on_error_callbacks(error)
    ensure
      invoke_after_run_callbacks
    end
    self
  end
end

#success?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ultra_marathon/abstract_runner.rb', line 38

def success?
  !!success
end