Class Mocha::ClassMethod
In: lib/mocha/class_method.rb
Parent: Object

Methods

Attributes

method  [R] 
stubbee  [R] 

Public Class methods

[Source]

    # 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

Public Instance methods

==(other)

Alias for eql?

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # File lib/mocha/class_method.rb, line 25
25:     def mock
26:       stubbee.mocha
27:     end

[Source]

    # File lib/mocha/class_method.rb, line 43
43:     def remove_new_method
44:       stubbee.__metaclass__.send(:remove_method, method)
45:     end

[Source]

    # 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

[Source]

    # File lib/mocha/class_method.rb, line 14
14:     def stub
15:       hide_original_method
16:       define_new_method
17:     end

[Source]

    # File lib/mocha/class_method.rb, line 75
75:     def to_s
76:       "#{stubbee}.#{method}"
77:     end

[Source]

    # File lib/mocha/class_method.rb, line 19
19:     def unstub
20:       remove_new_method
21:       restore_original_method
22:       stubbee.reset_mocha
23:     end

[Validate]