Class: UltraMarathon::AbstractRunner
- Inherits:
-
Object
- Object
- UltraMarathon::AbstractRunner
- Includes:
- Callbacks, Instrumentation, Logging
- Defined in:
- lib/ultra_marathon/abstract_runner.rb
Constant Summary
Constants included from Instrumentation
Instance Attribute Summary collapse
-
#success ⇒ Object
Returns the value of attribute success.
Class Method Summary collapse
-
.run(name = :main, options = {}, &block) ⇒ Object
This is where the magic happens.
- .run_blocks ⇒ Object
Instance Method Summary collapse
-
#reset ⇒ Object
Resets success to being true, unsets the failed sub_runners to [], and sets the unrun sub_runners to be the uncompleted/failed ones.
-
#run! ⇒ Object
Runs the run block safely in the context of the instance.
- #success? ⇒ Boolean
Methods included from Instrumentation
Methods included from Logging
Instance Attribute Details
#success ⇒ Object
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, ={}, &block) name = name.to_sym if !run_blocks.key? name [:name] = name self.run_blocks[name] = [, block] else raise NameError.new("Run block named #{name} already exists!") end end |
.run_blocks ⇒ Object
91 92 93 |
# File 'lib/ultra_marathon/abstract_runner.rb', line 91 def run_blocks @run_blocks ||= Hash.new end |
Instance Method Details
#reset ⇒ Object
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
38 39 40 |
# File 'lib/ultra_marathon/abstract_runner.rb', line 38 def success? !!success end |