class Shoulda::Matchers::Doublespeak::Double

Attributes

calls[R]
implementation[R]
klass[R]
method_name[R]
original_method[R]

Public Class Methods

new(klass, method_name, implementation) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 7
def initialize(klass, method_name, implementation)
  @klass = klass
  @method_name = method_name
  @implementation = implementation
  @activated = false
  @calls = []
end

Public Instance Methods

activate() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 23
def activate
  unless @activated
    store_original_method
    replace_method_with_double
    @activated = true
  end
end
call_original_method(object, args, block) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 42
def call_original_method(object, args, block)
  if original_method
    original_method.bind(object).call(*args, &block)
  end
end
deactivate() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 31
def deactivate
  if @activated
    restore_original_method
    @activated = false
  end
end
record_call(args, block) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 38
def record_call(args, block)
  calls << MethodCall.new(args, block)
end
to_return(value = nil, &block) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 15
def to_return(value = nil, &block)
  if block
    implementation.returns(&block)
  else
    implementation.returns(value)
  end
end

Private Instance Methods

replace_method_with_double() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 56
def replace_method_with_double
  implementation = @implementation
  double = self

  klass.__send__(:define_method, method_name) do |*args, &block|
    implementation.call(double, self, args, block)
  end
end
restore_original_method() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 65
def restore_original_method
  original_method = @original_method
  klass.__send__(:define_method, method_name) do |*args, &block|
    original_method.bind(self).call(*args, &block)
  end
end
store_original_method() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 52
def store_original_method
  @original_method = klass.instance_method(method_name)
end