| 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("def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end", __FILE__, __LINE__)
41: end
# File lib/mocha/class_method.rb, line 68
68: def eql?(other)
69: return false unless (other.class == self.class)
70: (stubbee.object_id == other.stubbee.object_id) and (method == other.method)
71: end
# File lib/mocha/class_method.rb, line 58
58: def hidden_method
59: if RUBY_VERSION < '1.9'
60: method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
61: else
62: method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
63: end
64: hidden_method = "__stubba__#{method_name}__stubba__"
65: RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym
66: 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 79
79: def method_exists?(method)
80: symbol = method.to_sym
81: metaclass = stubbee.__metaclass__
82: metaclass.public_method_defined?(symbol) || metaclass.protected_method_defined?(symbol) || metaclass.private_method_defined?(symbol)
83: end
# File lib/mocha/class_method.rb, line 43
43: def remove_new_method
44: stubbee.__metaclass__.send(:remove_method, method)
45: end
# File lib/mocha/class_method.rb, line 47
47: def restore_original_method
48: if method_exists?(hidden_method)
49: begin
50: stubbee.__metaclass__.send(:alias_method, method, hidden_method)
51: stubbee.__metaclass__.send(:remove_method, hidden_method)
52: rescue NameError
53: # deal with nasties like ActiveRecord::Associations::AssociationProxy
54: end
55: end
56: end
# File lib/mocha/class_method.rb, line 14
14: def stub
15: hide_original_method
16: define_new_method
17: end