Class: WebMock::StubRegistry
- Inherits:
-
Object
- Object
- WebMock::StubRegistry
- Includes:
- Singleton
- Defined in:
- lib/webmock/stub_registry.rb
Instance Attribute Summary collapse
-
#request_stubs ⇒ Object
Returns the value of attribute request_stubs.
Instance Method Summary collapse
- #global_stubs ⇒ Object
-
#initialize ⇒ StubRegistry
constructor
A new instance of StubRegistry.
- #register_global_stub(order = :before_local_stubs, &block) ⇒ Object
- #register_request_stub(stub) ⇒ Object
- #registered_request?(request_signature) ⇒ Boolean
- #remove_request_stub(stub) ⇒ Object
- #reset! ⇒ Object
- #response_for_request(request_signature) ⇒ Object
Constructor Details
#initialize ⇒ StubRegistry
Returns a new instance of StubRegistry.
10 11 12 |
# File 'lib/webmock/stub_registry.rb', line 10 def initialize reset! end |
Instance Attribute Details
#request_stubs ⇒ Object
Returns the value of attribute request_stubs.
8 9 10 |
# File 'lib/webmock/stub_registry.rb', line 8 def request_stubs @request_stubs end |
Instance Method Details
#global_stubs ⇒ Object
14 15 16 |
# File 'lib/webmock/stub_registry.rb', line 14 def global_stubs @global_stubs ||= Hash.new { |h, k| h[k] = [] } end |
#register_global_stub(order = :before_local_stubs, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/webmock/stub_registry.rb', line 22 def register_global_stub(order = :before_local_stubs, &block) unless %i[before_local_stubs after_local_stubs].include?(order) raise ArgumentError.new("Wrong order. Use :before_local_stubs or :after_local_stubs") end # This hash contains the responses returned by the block, # keyed by the exact request (using the object_id). # That way, there's no race condition in case #to_return # doesn't run immediately after stub.with. responses = {} response_lock = Mutex.new stub = ::WebMock::RequestStub.new(:any, ->(uri) { true }).with { |request| update_response = -> { responses[request.object_id] = yield(request) } # The block can recurse, so only lock if we don't already own it if response_lock.owned? update_response.call else response_lock.synchronize(&update_response) end }.to_return(lambda { |request| response_lock.synchronize { responses.delete(request.object_id) } }) global_stubs[order].push stub end |
#register_request_stub(stub) ⇒ Object
50 51 52 53 |
# File 'lib/webmock/stub_registry.rb', line 50 def register_request_stub(stub) request_stubs.insert(0, stub) stub end |
#registered_request?(request_signature) ⇒ Boolean
61 62 63 |
# File 'lib/webmock/stub_registry.rb', line 61 def registered_request?(request_signature) request_stub_for(request_signature) end |
#remove_request_stub(stub) ⇒ Object
55 56 57 58 59 |
# File 'lib/webmock/stub_registry.rb', line 55 def remove_request_stub(stub) if not request_stubs.delete(stub) raise "Request stub \n\n #{stub.to_s} \n\n is not registered." end end |
#reset! ⇒ Object
18 19 20 |
# File 'lib/webmock/stub_registry.rb', line 18 def reset! self.request_stubs = [] end |
#response_for_request(request_signature) ⇒ Object
65 66 67 68 |
# File 'lib/webmock/stub_registry.rb', line 65 def response_for_request(request_signature) stub = request_stub_for(request_signature) stub ? evaluate_response_for_request(stub.response, request_signature) : nil end |