Module: Crspec::Mock::Interceptor
- Defined in:
- lib/crspec/mock/interceptor.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/crspec/mock/interceptor.rb', line 9
def method_missing(method_name, *args, **kwargs, &block)
space = Fiber[Space::STORAGE_KEY]
if space && (stub = space.fetch_stub(self, method_name))
stub.call(*args, **kwargs, &block)
else
super
end
end
|
Class Method Details
.add_intercept_method(method_name) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/crspec/mock/interceptor.rb', line 24
def add_intercept_method(method_name)
method_sym = method_name.to_sym
@mutex.synchronize do
return if @intercepted_methods.key?(method_sym)
return if instance_methods(false).include?(method_sym) ||
private_instance_methods(false).include?(method_sym)
define_method(method_sym) do |*args, **kwargs, &block|
space = Fiber[Space::STORAGE_KEY]
if space && (stub = space.fetch_stub(self, method_sym))
stub.call(*args, **kwargs, &block)
else
super(*args, **kwargs, &block)
end
end
@intercepted_methods[method_sym] = true
end
end
|
.cleanup! ⇒ Object
Removes all dynamically-defined intercept methods so they do not
accumulate for the lifetime of the process (e.g. across multiple
embedded runner invocations). Stub lookups fall back to
method_missing until re-added.
47
48
49
50
51
52
53
54
|
# File 'lib/crspec/mock/interceptor.rb', line 47
def cleanup!
@mutex.synchronize do
@intercepted_methods.each_key do |method_sym|
remove_method(method_sym) if instance_methods(false).include?(method_sym)
end
@intercepted_methods.clear
end
end
|
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
18
19
20
21
|
# File 'lib/crspec/mock/interceptor.rb', line 18
def respond_to_missing?(method_name, include_private = false)
space = Fiber[Space::STORAGE_KEY]
space&.fetch_stub(self, method_name) || super
end
|