Class: Stubba::ClassMethod
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(stubbee, method) ⇒ ClassMethod
Returns a new instance of ClassMethod.
9
10
11
|
# File 'lib/stubba/class_method.rb', line 9
def initialize(stubbee, method)
@stubbee, @method = stubbee, method
end
|
Instance Attribute Details
Returns the value of attribute method.
7
8
9
|
# File 'lib/stubba/class_method.rb', line 7
def method
@method
end
|
Returns the value of attribute stubbee.
7
8
9
|
# File 'lib/stubba/class_method.rb', line 7
def stubbee
@stubbee
end
|
Instance Method Details
#cannot_replace_method_error ⇒ Object
48
49
50
|
# File 'lib/stubba/class_method.rb', line 48
def cannot_replace_method_error
Test::Unit::AssertionFailedError.new("Cannot replace #{method} because it is not defined in #{stubbee}.")
end
|
#define_new_method ⇒ Object
32
33
34
|
# File 'lib/stubba/class_method.rb', line 32
def define_new_method
stubbee.metaclass.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
52
53
54
55
|
# File 'lib/stubba/class_method.rb', line 52
def eql?(other)
return false unless (other.class == self.class)
(stubbee == other.stubbee) and (method == other.method)
end
|
#hidden_method ⇒ Object
44
45
46
|
# File 'lib/stubba/class_method.rb', line 44
def hidden_method
"__stubba__#{method.to_s.sub('!', '_exclamation_mark').sub('?', '_question_mark')}__stubba__"
end
|
#hide_original_method ⇒ Object
28
29
30
|
# File 'lib/stubba/class_method.rb', line 28
def hide_original_method
stubbee.metaclass.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.metaclass.method_defined?(method)
end
|
24
25
26
|
# File 'lib/stubba/class_method.rb', line 24
def mock
stubbee.mocha
end
|
#remove_new_method ⇒ Object
36
37
38
|
# File 'lib/stubba/class_method.rb', line 36
def remove_new_method
stubbee.metaclass.class_eval "remove_method :#{method}"
end
|
#restore_original_method ⇒ Object
40
41
42
|
# File 'lib/stubba/class_method.rb', line 40
def restore_original_method
stubbee.metaclass.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.metaclass.method_defined?(hidden_method)
end
|
13
14
15
16
|
# File 'lib/stubba/class_method.rb', line 13
def stub
hide_original_method
define_new_method
end
|
59
60
61
|
# File 'lib/stubba/class_method.rb', line 59
def to_s
"#{stubbee}.#{method}"
end
|
18
19
20
21
22
|
# File 'lib/stubba/class_method.rb', line 18
def unstub
remove_new_method
restore_original_method
stubbee.reset_mocha
end
|