class Shoulda::Matchers::Doublespeak::Double
@private
Attributes
Public Class Methods
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
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
Source
# File lib/shoulda/matchers/doublespeak/double.rb, line 21 def activated? @activated end
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
Source
# File lib/shoulda/matchers/doublespeak/double.rb, line 41 def deactivate if @activated restore_original_method @activated = false end end
Source
# File lib/shoulda/matchers/doublespeak/double.rb, line 48 def record_call(call) calls << call end
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
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
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
Source
# File lib/shoulda/matchers/doublespeak/double.rb, line 65 def store_original_method world.store_original_method_for(klass, method_name) end