Class: Nanny::Progress
- Inherits:
-
Object
- Object
- Nanny::Progress
- Defined in:
- lib/nanny/progress.rb
Instance Attribute Summary collapse
-
#childs ⇒ Object
readonly
Returns the value of attribute childs.
-
#my_done ⇒ Object
readonly
Returns the value of attribute my_done.
-
#my_todo ⇒ Object
readonly
Returns the value of attribute my_todo.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #child ⇒ Object
- #complete! ⇒ Object
- #done!(n) ⇒ Object
-
#initialize(parent = nil, &block) ⇒ Progress
constructor
A new instance of Progress.
- #notify! ⇒ Object
- #step ⇒ Object
- #todo(n) ⇒ Object
- #total_done ⇒ Object
- #total_todo ⇒ Object
Constructor Details
#initialize(parent = nil, &block) ⇒ Progress
Returns a new instance of Progress.
11 12 13 14 15 16 17 |
# File 'lib/nanny/progress.rb', line 11 def initialize(parent = nil, &block) @listener = block @parent = parent @my_todo = 0 @my_done = 0 @childs = [] end |
Instance Attribute Details
#childs ⇒ Object (readonly)
Returns the value of attribute childs.
6 7 8 |
# File 'lib/nanny/progress.rb', line 6 def childs @childs end |
#my_done ⇒ Object (readonly)
Returns the value of attribute my_done.
8 9 10 |
# File 'lib/nanny/progress.rb', line 8 def my_done @my_done end |
#my_todo ⇒ Object (readonly)
Returns the value of attribute my_todo.
7 8 9 |
# File 'lib/nanny/progress.rb', line 7 def my_todo @my_todo end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/nanny/progress.rb', line 9 def parent @parent end |
Instance Method Details
#child ⇒ Object
44 45 46 47 48 |
# File 'lib/nanny/progress.rb', line 44 def child Progress.new(self).tap do |child| @childs << child end end |
#complete! ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/nanny/progress.rb', line 36 def complete! if @my_done != my_todo @my_done = my_todo childs.each(&:complete!) notify! end end |
#done!(n) ⇒ Object
31 32 33 34 |
# File 'lib/nanny/progress.rb', line 31 def done!(n) @my_done += n notify! end |
#notify! ⇒ Object
58 59 60 61 |
# File 'lib/nanny/progress.rb', line 58 def notify! parent.notify! if parent @listener.call(self) if @listener end |
#step ⇒ Object
19 20 21 22 23 24 |
# File 'lib/nanny/progress.rb', line 19 def step todo(1) result = yield done!(1) result end |
#todo(n) ⇒ Object
26 27 28 29 |
# File 'lib/nanny/progress.rb', line 26 def todo(n) @my_todo += n notify! end |
#total_done ⇒ Object
54 55 56 |
# File 'lib/nanny/progress.rb', line 54 def total_done my_done + childs.sum(&:total_done) end |
#total_todo ⇒ Object
50 51 52 |
# File 'lib/nanny/progress.rb', line 50 def total_todo my_todo + childs.sum(&:total_todo) end |