Class: Mocha::ClassMethod

Inherits:
Object show all
Defined in:
lib/mocha/class_method.rb

Direct Known Subclasses

AnyInstanceMethod, InstanceMethod, ModuleMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stubbee, method) ⇒ ClassMethod


9
10
11
12
# File 'lib/mocha/class_method.rb', line 9

def initialize(stubbee, method)
  @stubbee, @original_method = stubbee, nil
  @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method


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

def method
  @method
end

#stubbeeObject (readonly)

Returns the value of attribute stubbee


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

def stubbee
  @stubbee
end

Instance Method Details

#define_new_methodObject


55
56
57
58
59
60
61
# File 'lib/mocha/class_method.rb', line 55

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

#hide_original_methodObject


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mocha/class_method.rb', line 36

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

#matches?(other) ⇒ Boolean


77
78
79
80
# File 'lib/mocha/class_method.rb', line 77

def matches?(other)
  return false unless (other.class == self.class)
  (stubbee.object_id == other.stubbee.object_id) and (method == other.method)
end

#method_exists?(method) ⇒ Boolean


88
89
90
91
92
# File 'lib/mocha/class_method.rb', line 88

def method_exists?(method)
  symbol = method.to_sym
  __metaclass__ = stubbee.__metaclass__
  __metaclass__.public_method_defined?(symbol) || __metaclass__.protected_method_defined?(symbol) || __metaclass__.private_method_defined?(symbol)
end

#mockObject


28
29
30
# File 'lib/mocha/class_method.rb', line 28

def mock
  stubbee.mocha
end

#remove_new_methodObject


63
64
65
# File 'lib/mocha/class_method.rb', line 63

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

#reset_mochaObject


32
33
34
# File 'lib/mocha/class_method.rb', line 32

def reset_mocha
  stubbee.reset_mocha
end

#restore_original_methodObject


67
68
69
70
71
72
73
74
75
# File 'lib/mocha/class_method.rb', line 67

def restore_original_method
  if @original_method && @original_method.owner == stubbee.__metaclass__
    original_method = @original_method
    stubbee.__metaclass__.send(:define_method, method) do |*args, &block|
      original_method.call(*args, &block)
    end
    stubbee.__metaclass__.send(@original_visibility, method)
  end
end

#stubObject


14
15
16
17
# File 'lib/mocha/class_method.rb', line 14

def stub
  hide_original_method
  define_new_method
end

#to_sObject


84
85
86
# File 'lib/mocha/class_method.rb', line 84

def to_s
  "#{stubbee}.#{method}"
end

#unstubObject


19
20
21
22
23
24
25
26
# File 'lib/mocha/class_method.rb', line 19

def unstub
  remove_new_method
  restore_original_method
  mock.unstub(method.to_sym)
  unless mock.any_expectations?
    reset_mocha
  end
end