Skip to content

Commit

Permalink
Refactor WebMock::Util::HashCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
akmhmgc committed Aug 18, 2022
1 parent 8265143 commit 2a70160
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/webmock/util/hash_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@ module WebMock
module Util
class HashCounter
attr_accessor :hash

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

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

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

def select(&block)
return unless block_given?

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

0 comments on commit 2a70160

Please sign in to comment.