class Shoulda::Matchers::Doublespeak::Double

@private

Attributes

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

Public Class Methods

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

  if world.doubles_activated?
    activate
  end
end

Public Instance Methods

activate() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 33
def activate
  unless @activated
    store_original_method
    replace_method_with_double
    @activated = true
  end
end
activated?() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 21
def activated?
  @activated
end
call_original_method(call) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 52
def call_original_method(call)
  unbound_method = world.original_method_for(klass, call.method_name)

  if unbound_method
    unbound_method.bind(call.object).call(*call.args, &call.block)
  end
end
deactivate() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 41
def deactivate
  if @activated
    restore_original_method
    @activated = false
  end
end
record_call(call) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 48
def record_call(call)
  calls << call
end
to_return(value = nil, &block) click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 25
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 69
def replace_method_with_double
  double = self
  implementation = @implementation
  _method_name = method_name

  if klass.instance_methods(false).include?(method_name)
    klass.__send__(:remove_method, method_name)
  end

  klass.__send__(:define_method, method_name) do |*args, &block|
    call = MethodCall.new(
      double: double,
      object: self,
      method_name: _method_name,
      args: args,
      block: block,
      caller: caller
    )
    implementation.call(call)
  end
end
restore_original_method() click to toggle source
# File lib/shoulda/matchers/doublespeak/double.rb, line 91
def restore_original_method
  original_method = world.original_method_for(klass, method_name)

  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 65
def store_original_method
  world.store_original_method_for(klass, method_name)
end