| Class | Mocha::ClassMethod |
| In: |
lib/mocha/class_method.rb
|
| Parent: | Object |
| method | [R] | |
| stubbee | [R] |
# File lib/mocha/class_method.rb, line 9
9: def initialize(stubbee, method)
10: @stubbee = stubbee
11: @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
12: end
# File lib/mocha/class_method.rb, line 39
39: def define_new_method
40: stubbee.__metaclass__.class_eval(%{
41: def #{method}(*args, &block)
42: mocha.method_missing(:#{method}, *args, &block)
43: end
44: }, __FILE__, __LINE__)
45: end
# File lib/mocha/class_method.rb, line 72
72: def eql?(other)
73: return false unless (other.class == self.class)
74: (stubbee.object_id == other.stubbee.object_id) and (method == other.method)
75: end
# File lib/mocha/class_method.rb, line 62
62: def hidden_method
63: if RUBY_VERSION < '1.9'
64: method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
65: else
66: method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
67: end
68: hidden_method = "__stubba__#{method_name}__stubba__"
69: RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym
70: end
# File lib/mocha/class_method.rb, line 29
29: def hide_original_method
30: if method_exists?(method)
31: begin
32: stubbee.__metaclass__.send(:alias_method, hidden_method, method)
33: rescue NameError
34: # deal with nasties like ActiveRecord::Associations::AssociationProxy
35: end
36: end
37: end
# File lib/mocha/class_method.rb, line 83
83: def method_exists?(method)
84: symbol = method.to_sym
85: metaclass = stubbee.__metaclass__
86: metaclass.public_method_defined?(symbol) || metaclass.protected_method_defined?(symbol) || metaclass.private_method_defined?(symbol)
87: end
# File lib/mocha/class_method.rb, line 47
47: def remove_new_method
48: stubbee.__metaclass__.send(:remove_method, method)
49: end
# File lib/mocha/class_method.rb, line 51
51: def restore_original_method
52: if method_exists?(hidden_method)
53: begin
54: stubbee.__metaclass__.send(:alias_method, method, hidden_method)
55: stubbee.__metaclass__.send(:remove_method, hidden_method)
56: rescue NameError
57: # deal with nasties like ActiveRecord::Associations::AssociationProxy
58: end
59: end
60: end
# File lib/mocha/class_method.rb, line 14
14: def stub
15: hide_original_method
16: define_new_method
17: end