Skip to content

Commit

Permalink
Merge pull request #1000 from akmhmgc/refactor
Browse files Browse the repository at this point in the history
Refactor WebMock::Util::HashCounter
  • Loading branch information
bblimke authored Aug 18, 2022
2 parents 8265143 + 2a70160 commit 833291d
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 833291d

Please sign in to comment.