Class: Console::Measure

Inherits:
Object
  • Object
show all
Defined in:
lib/console/measure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, subject, total = 0) ⇒ Measure

Returns a new instance of Measure.



23
24
25
26
27
28
29
30
31
# File 'lib/console/measure.rb', line 23

def initialize(output, subject, total = 0)
  @output = output
  @subject = subject
  
  @start_time = Time.now
  
  @current = 0
  @total = total
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



35
36
37
# File 'lib/console/measure.rb', line 35

def current
  @current
end

#subjectObject (readonly)

Returns the value of attribute subject.



33
34
35
# File 'lib/console/measure.rb', line 33

def subject
  @subject
end

#totalObject (readonly)

Returns the value of attribute total.



37
38
39
# File 'lib/console/measure.rb', line 37

def total
  @total
end

Instance Method Details

#average_durationObject



51
52
53
54
55
# File 'lib/console/measure.rb', line 51

def average_duration
  if @current > 0
    duration / @current
  end
end

#durationObject



39
40
41
# File 'lib/console/measure.rb', line 39

def duration
  Time.now - @start_time
end

#estimated_remaining_timeObject



57
58
59
60
61
# File 'lib/console/measure.rb', line 57

def estimated_remaining_time
  if average_duration = self.average_duration
    average_duration * remaining
  end
end

#increment(amount = 1) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/console/measure.rb', line 63

def increment(amount = 1)
  @current += amount
  
  @output.info(@subject, self) {Event::Progress.new(@current, @total)}
  
  return self
end

#mark(*arguments) ⇒ Object



79
80
81
# File 'lib/console/measure.rb', line 79

def mark(*arguments)
  @output.info(@subject, *arguments)
end

#progressObject



43
44
45
# File 'lib/console/measure.rb', line 43

def progress
  @current.to_f / @total.to_f
end

#remainingObject



47
48
49
# File 'lib/console/measure.rb', line 47

def remaining
  @total - @current
end

#resize(total) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/console/measure.rb', line 71

def resize(total)
  @total = total
  
  @output.info(@subject, self) {Event::Progress.new(@current, @total)}
  
  return self
end

#to_sObject



83
84
85
86
87
88
89
# File 'lib/console/measure.rb', line 83

def to_s
  if estimated_remaining_time = self.estimated_remaining_time
    "#{@current}/#{@total} completed in #{self.formatted_duration self.duration}, #{self.formatted_duration estimated_remaining_time} remaining."
  else
    "#{@current}/#{@total} completed, waiting for estimate..."
  end
end