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
69
# 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
  @created_at = Time.now
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

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
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



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

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



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

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

#durationObject



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

def duration
  @total_duration / self.count
end

#merge!(tc) ⇒ Object



108
109
110
111
112
113
# File 'lib/quicky/timer.rb', line 108

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



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

def to_hash
  {
      :created_at => self.created_at,
      :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



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

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