| 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, @method = stubbee, method
11: end
# File lib/mocha/class_method.rb, line 38
38: def define_new_method
39: stubbee.__metaclass__.class_eval("def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end", __FILE__, __LINE__)
40: end
# File lib/mocha/class_method.rb, line 65
65: def eql?(other)
66: return false unless (other.class == self.class)
67: (stubbee.object_id == other.stubbee.object_id) and (method == other.method)
68: end
# File lib/mocha/class_method.rb, line 56
56: def hidden_method
57: if RUBY_VERSION < '1.9'
58: method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
59: else
60: method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
61: end
62: "__stubba__#{method_name}__stubba__"
63: end
# File lib/mocha/class_method.rb, line 28
28: def hide_original_method
29: if method_exists?(method)
30: begin
31: stubbee.__metaclass__.class_eval("alias_method :#{hidden_method}, :#{method}", __FILE__, __LINE__)
32: rescue NameError
33: # deal with nasties like ActiveRecord::Associations::AssociationProxy
34: end
35: end
36: end
# File lib/mocha/class_method.rb, line 76
76: def method_exists?(method)
77: existing_methods = []
78: existing_methods += stubbee.public_methods(true) - stubbee.superclass.public_methods(true)
79: existing_methods += stubbee.protected_methods(true) - stubbee.superclass.protected_methods(true)
80: existing_methods += stubbee.private_methods(true) - stubbee.superclass.private_methods(true)
81: existing_methods.any? { |m| m.to_s == method.to_s }
82: end
# File lib/mocha/class_method.rb, line 42
42: def remove_new_method
43: stubbee.__metaclass__.class_eval("remove_method :#{method}", __FILE__, __LINE__)
44: end
# File lib/mocha/class_method.rb, line 46
46: def restore_original_method
47: if method_exists?(hidden_method)
48: begin
49: stubbee.__metaclass__.class_eval("alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}", __FILE__, __LINE__)
50: rescue NameError
51: # deal with nasties like ActiveRecord::Associations::AssociationProxy
52: end
53: end
54: end
# File lib/mocha/class_method.rb, line 13
13: def stub
14: hide_original_method
15: define_new_method
16: end