Class: Mocha::AnyInstanceMethod

Inherits:
ClassMethod show all
Defined in:
lib/mocha/any_instance_method.rb

Instance Attribute Summary

Attributes inherited from ClassMethod

#method, #stubbee

Instance Method Summary collapse

Methods inherited from ClassMethod

#initialize, #matches?, #stub, #to_s, #unstub

Constructor Details

This class inherits a constructor from Mocha::ClassMethod

Instance Method Details

#define_new_methodObject


34
35
36
37
38
39
40
# File 'lib/mocha/any_instance_method.rb', line 34

def define_new_method
  stubbee.class_eval(%{
    def #{method}(*args, &block)
      self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
    end
  }, __FILE__, __LINE__)
end

#hide_original_methodObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mocha/any_instance_method.rb', line 15

def hide_original_method
  if method_exists?(method)
    begin
      @original_method = stubbee.instance_method(method)
      if @original_method && @original_method.owner == stubbee
        @original_visibility = :public
        if stubbee.protected_instance_methods.include?(method)
          @original_visibility = :protected
        elsif stubbee.private_instance_methods.include?(method)
          @original_visibility = :private
        end
        stubbee.send(:remove_method, method)
      end
    rescue NameError
      # deal with nasties like ActiveRecord::Associations::AssociationProxy
    end
  end
end

#method_exists?(method) ⇒ Boolean


53
54
55
56
57
58
# File 'lib/mocha/any_instance_method.rb', line 53

def method_exists?(method)
  return true if stubbee.public_instance_methods(false).include?(method)
  return true if stubbee.protected_instance_methods(false).include?(method)
  return true if stubbee.private_instance_methods(false).include?(method)
  return false
end

#mockObject


7
8
9
# File 'lib/mocha/any_instance_method.rb', line 7

def mock
  stubbee.any_instance.mocha
end

#remove_new_methodObject


42
43
44
# File 'lib/mocha/any_instance_method.rb', line 42

def remove_new_method
  stubbee.send(:remove_method, method)
end

#reset_mochaObject


11
12
13
# File 'lib/mocha/any_instance_method.rb', line 11

def reset_mocha
  stubbee.any_instance.reset_mocha
end

#restore_original_methodObject


46
47
48
49
50
51
# File 'lib/mocha/any_instance_method.rb', line 46

def restore_original_method
  if @original_method && @original_method.owner == stubbee
    stubbee.send(:define_method, method, @original_method)
    stubbee.send(@original_visibility, method)
  end
end