Class: Mocha::AnyInstanceMethod
Instance Attribute Summary
Attributes inherited from ClassMethod
#method, #stubbee
Instance Method Summary
collapse
Methods inherited from ClassMethod
#eql?, #hidden_method, #initialize, #stub, #to_s
Instance Method Details
#define_new_method ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/mocha/any_instance_method.rb', line 27
def define_new_method
stubbee.class_eval(%{
def #{method}(*args, &block)
self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
end
}, __FILE__, __LINE__)
end
|
#hide_original_method ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/mocha/any_instance_method.rb', line 17
def hide_original_method
if method_exists?(method)
begin
stubbee.send(:alias_method, hidden_method, method)
rescue NameError
end
end
end
|
#method_exists?(method) ⇒ Boolean
50
51
52
53
54
55
|
# File 'lib/mocha/any_instance_method.rb', line 50
def method_exists?(method)
return true if stubbee.public_instance_methods(false).include?(method)
return true if stubbee.protected_instance_methods(false).include?(method)
return true if stubbee.private_instance_methods(false).include?(method)
return false
end
|
13
14
15
|
# File 'lib/mocha/any_instance_method.rb', line 13
def mock
stubbee.any_instance.mocha
end
|
#remove_new_method ⇒ Object
35
36
37
|
# File 'lib/mocha/any_instance_method.rb', line 35
def remove_new_method
stubbee.send(:remove_method, method)
end
|
#restore_original_method ⇒ Object
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/mocha/any_instance_method.rb', line 39
def restore_original_method
if method_exists?(hidden_method)
begin
stubbee.send(:alias_method, method, hidden_method)
stubbee.send(:remove_method, hidden_method)
rescue NameError
end
end
end
|
7
8
9
10
11
|
# File 'lib/mocha/any_instance_method.rb', line 7
def unstub
remove_new_method
restore_original_method
stubbee.any_instance.reset_mocha
end
|