Class: LogfileInterval::Util::Counter

Inherits:
Hash
  • Object
show all
Defined in:
lib/logfile_interval/util/counter.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/logfile_interval/util/counter.rb', line 35

def [](key)
  self.fetch(key, 0)
end

#add(key, num) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/logfile_interval/util/counter.rb', line 23

def add(key, num)
  if self.has_key?(key)
    self[key] += num
  else
    self[key] = num
  end
end

#increment(key) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/logfile_interval/util/counter.rb', line 4

def increment(key)
  if self.has_key?(key)
    self[key] += 1
  else
    self[key] = 1
  end
end

#increment_subkey(key, subkey) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/logfile_interval/util/counter.rb', line 12

def increment_subkey(key, subkey)
  if self.has_key?(key) && !self[key].is_a?(Counter)
    raise "Value for #{key} is not a Counter"
  end

  unless self.has_key?(key)
    self[key] = Counter.new
  end
  self[key].increment(subkey)
end

#merge(c) ⇒ Object



39
40
41
42
43
# File 'lib/logfile_interval/util/counter.rb', line 39

def merge(c)
  c.keys.each do |k|
    self.add c[k]
  end
end

#set(key, num) ⇒ Object



31
32
33
# File 'lib/logfile_interval/util/counter.rb', line 31

def set(key, num)
  self[key] = num
end