Class: DInstaller::Progress

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(total_steps) ⇒ Progress

Constructor

Parameters:

  • total number of steps



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_stepsInteger (readonly)

Total number of steps

Returns:



74
75
76
# File 'lib/dinstaller/progress.rb', line 74

def total_steps
  @total_steps
end

Instance Method Details

#current_stepStep?

Current progress step, if any

Returns:

  • nil if the progress is already finished or not stated yet.



91
92
93
94
95
# File 'lib/dinstaller/progress.rb', line 91

def current_step
  return nil if finished?

  @current_step
end

#finishObject

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

Returns:



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

Parameters:



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

Parameters:



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.

Parameters:

  • description of the step

Returns:

  • result of the given block or nil if no block is given



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