class Shoulda::Matchers::Doublespeak::Double

@private

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 8
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 24
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 43
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 32
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 39
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 16
def to_return(value = nil, &block)
  if block
    implementation.returns(&block)
  else
    implementation.returns(value)
  end
end

Protected Instance Methods

replace_method_with_double() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 57
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 66
def restore_original_method
  original_method = @original_method
  klass.__send__(:remove_method, method_name)
  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 53
def store_original_method
  @original_method = klass.instance_method(method_name)
end