Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/redistat/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#merge_and_incr(hash) ⇒ Object



3
4
5
# File 'lib/redistat/core_ext/hash.rb', line 3

def merge_and_incr(hash)
  self.clone.merge_and_incr!(hash)
end

#merge_and_incr!(hash) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/redistat/core_ext/hash.rb', line 7

def merge_and_incr!(hash)
  raise ArgumentError unless hash.is_a?(Hash)
  hash.each do |key, value|
    self[key] = value unless self.set_or_incr(key, value)
  end
  self
end

#set_or_incr(key, value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/redistat/core_ext/hash.rb', line 15

def set_or_incr(key, value)
  return false unless value.is_a?(Numeric)
  self[key] = 0 unless self.has_key?(key)
  return false unless self[key].is_a?(Numeric)
  self[key] += value
  true
end