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, @method = stubbee, method
11:     end

Public Instance methods

==(other)

Alias for eql?

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

    # 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

[Source]

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

[Source]

    # 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

[Source]

    # 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

[Source]

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

[Source]

    # File lib/mocha/class_method.rb, line 72
72:     def to_s
73:       "#{stubbee}.#{method}"
74:     end

[Source]

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

[Validate]