Class: WebMock::Util::HashCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/webmock/util/hash_counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHashCounter

Returns a new instance of HashCounter.



10
11
12
13
14
15
# File 'lib/webmock/util/hash_counter.rb', line 10

def initialize
  self.hash = Hash.new(0)
  @order = {}
  @max = 0
  @lock = ::Mutex.new
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



8
9
10
# File 'lib/webmock/util/hash_counter.rb', line 8

def hash
  @hash
end

Instance Method Details

#each(&block) ⇒ Object



38
39
40
41
42
# File 'lib/webmock/util/hash_counter.rb', line 38

def each(&block)
  @order.to_a.sort_by { |a| a[1] }.each do |a|
    yield(a[0], hash[a[0]])
  end
end

#get(key) ⇒ Object



24
25
26
27
28
# File 'lib/webmock/util/hash_counter.rb', line 24

def get(key)
  @lock.synchronize do
    hash[key]
  end
end

#put(key, num = 1) ⇒ Object



17
18
19
20
21
22
# File 'lib/webmock/util/hash_counter.rb', line 17

def put(key, num=1)
  @lock.synchronize do
    hash[key] += num
    @order[key] = @max += 1
  end
end

#select(&block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/webmock/util/hash_counter.rb', line 30

def select(&block)
  return unless block_given?

  @lock.synchronize do
    hash.select(&block)
  end
end