Class: Quicky::TimeCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/quicky/timer.rb

Constant Summary collapse

INT_MAX =
((2 ** (32 - 2)) - 1)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ TimeCollector

Returns a new instance of TimeCollector.



61
62
63
64
65
66
67
68
# File 'lib/quicky/timer.rb', line 61

def initialize(name)
  @name = name
  @collector = []
  @total_duration = 0.0
  @max_duration = 0.0
  @min_duration = INT_MAX
  @count = 0
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



59
60
61
# File 'lib/quicky/timer.rb', line 59

def count
  @count
end

#max_durationObject

Returns the value of attribute max_duration.



59
60
61
# File 'lib/quicky/timer.rb', line 59

def max_duration
  @max_duration
end

#min_durationObject

Returns the value of attribute min_duration.



59
60
61
# File 'lib/quicky/timer.rb', line 59

def min_duration
  @min_duration
end

#nameObject

Returns the value of attribute name.



59
60
61
# File 'lib/quicky/timer.rb', line 59

def name
  @name
end

#total_durationObject

Returns the value of attribute total_duration.



59
60
61
# File 'lib/quicky/timer.rb', line 59

def total_duration
  @total_duration
end

Class Method Details

.from_hash(h) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/quicky/timer.rb', line 98

def self.from_hash(h)
  t = TimeCollector.new(h['name'])
  h.each_pair do |k,v|
    t.instance_variable_set("@#{k}", v)
  end
  t
end

Instance Method Details

#<<(val) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/quicky/timer.rb', line 70

def <<(val)
  # pull out duration for totals
  @total_duration += val.duration
  update_max_min(val.duration)
  @collector << val
  @count += 1
end

#durationObject



83
84
85
# File 'lib/quicky/timer.rb', line 83

def duration
  @total_duration / self.count
end

#merge!(tc) ⇒ Object



106
107
108
109
110
111
# File 'lib/quicky/timer.rb', line 106

def merge!(tc)
  self.count += tc.count
  self.total_duration += tc.total_duration
  update_max_min(tc.max_duration)
  update_max_min(tc.min_duration)
end

#to_hashObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/quicky/timer.rb', line 87

def to_hash
  {
      name: self.name,
      count: self.count,
      duration: self.duration,
      total_duration: self.total_duration,
      max_duration: self.max_duration,
      min_duration: self.min_duration
  }
end

#update_max_min(duration) ⇒ Object



78
79
80
81
# File 'lib/quicky/timer.rb', line 78

def update_max_min(duration)
  @max_duration = duration if duration > @max_duration
  @min_duration = duration if duration < @min_duration
end