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.



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

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

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



6
7
8
# File 'lib/webmock/util/hash_counter.rb', line 6

def hash
  @hash
end

Instance Method Details

#each(&block) ⇒ Object



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

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



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

def get key
  @lock.synchronize do
    hash[key] || 0
  end
end

#put(key, num = 1) ⇒ Object



13
14
15
16
17
18
# File 'lib/webmock/util/hash_counter.rb', line 13

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

#select(&block) ⇒ Object



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

def select(&block)
  return unless block_given?
  @lock.synchronize do
    hash.select(&block)
  end
end