Class: DInstaller::Progress
- Inherits:
-
Object
- Object
- DInstaller::Progress
- Defined in:
- lib/dinstaller/progress.rb
Overview
progress.step(“Doing step3”) do # calls on_change callbacks, executes the given
progress.current_step.description # block and calls on_finish callbacks
end #=> "Doing step3"
progress.finished? #=> true
progress.current_step #=> nil
Defined Under Namespace
Classes: Step
Instance Attribute Summary collapse
-
#total_steps ⇒ Integer
readonly
Total number of steps.
Instance Method Summary collapse
-
#current_step ⇒ Step?
Current progress step, if any.
-
#finish ⇒ Object
Finishes the progress and runs the callbacks.
-
#finished? ⇒ Boolean
Whether the last step was already done.
-
#initialize(total_steps) ⇒ Progress
constructor
Constructor.
-
#on_change(&block) ⇒ Object
Adds a callback to be called when progress changes.
-
#on_finish(&block) ⇒ Object
Adds a callback to be called when progress finishes.
-
#step(description, &block) ⇒ Object?
Runs a progress step.
Constructor Details
#initialize(total_steps) ⇒ Progress
Constructor
79 80 81 82 83 84 85 86 |
# File 'lib/dinstaller/progress.rb', line 79 def initialize(total_steps) @total_steps = total_steps @current_step = nil @counter = 0 @finished = false @on_change_callbacks = [] @on_finish_callbacks = [] end |
Instance Attribute Details
#total_steps ⇒ Integer (readonly)
Total number of steps
74 75 76 |
# File 'lib/dinstaller/progress.rb', line 74 def total_steps @total_steps end |
Instance Method Details
#current_step ⇒ Step?
Current progress step, if any
91 92 93 94 95 |
# File 'lib/dinstaller/progress.rb', line 91 def current_step return nil if finished? @current_step end |
#finish ⇒ Object
Finishes the progress and runs the callbacks
This method can be called to force the progress to finish before of running all the steps.
130 131 132 133 |
# File 'lib/dinstaller/progress.rb', line 130 def finish @finished = true @on_finish_callbacks.each(&:call) end |
#finished? ⇒ Boolean
Whether the last step was already done
123 124 125 |
# File 'lib/dinstaller/progress.rb', line 123 def finished? total_steps == 0 || @finished end |
#on_change(&block) ⇒ Object
Adds a callback to be called when progress changes
138 139 140 |
# File 'lib/dinstaller/progress.rb', line 138 def on_change(&block) @on_change_callbacks << block end |
#on_finish(&block) ⇒ Object
Adds a callback to be called when progress finishes
145 146 147 |
# File 'lib/dinstaller/progress.rb', line 145 def on_finish(&block) @on_finish_callbacks << block end |
#step(description, &block) ⇒ Object?
Runs a progress step
It calls the ‘on_change` callbacks and then runs the given block, if any. It also calls `on_finish` callbacks after the last step.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dinstaller/progress.rb', line 106 def step(description, &block) return if finished? @counter += 1 @current_step = Step.new(@counter, description) @on_change_callbacks.each(&:call) result = block_given? ? block.call : nil finish if @counter == total_steps result end |