Class: Nanny::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/nanny/progress.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childsObject (readonly)

Returns the value of attribute childs.



6
7
8
# File 'lib/nanny/progress.rb', line 6

def childs
  @childs
end

#my_doneObject (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_todoObject (readonly)

Returns the value of attribute my_todo.



7
8
9
# File 'lib/nanny/progress.rb', line 7

def my_todo
  @my_todo
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/nanny/progress.rb', line 9

def parent
  @parent
end

Instance Method Details

#childObject



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

#stepObject



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_doneObject



54
55
56
# File 'lib/nanny/progress.rb', line 54

def total_done
  my_done + childs.sum(&:total_done)
end

#total_todoObject



50
51
52
# File 'lib/nanny/progress.rb', line 50

def total_todo
  my_todo + childs.sum(&:total_todo)
end