Class: Shoulda::Matchers::Doublespeak::Double

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/doublespeak/double.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(world, klass, method_name, implementation) ⇒ Double

Returns a new instance of Double.



8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



6
7
8
# File 'lib/shoulda/matchers/doublespeak/double.rb', line 6

def calls
  @calls
end

Instance Method Details

#activateObject



33
34
35
36
37
38
39
# 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?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/shoulda/matchers/doublespeak/double.rb', line 21

def activated?
  @activated
end

#call_original_method(call) ⇒ Object



52
53
54
55
56
57
58
# 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

#deactivateObject



41
42
43
44
45
46
# File 'lib/shoulda/matchers/doublespeak/double.rb', line 41

def deactivate
  if @activated
    restore_original_method
    @activated = false
  end
end

#record_call(call) ⇒ Object



48
49
50
# File 'lib/shoulda/matchers/doublespeak/double.rb', line 48

def record_call(call)
  calls << call
end

#to_return(value = nil, &block) ⇒ Object



25
26
27
28
29
30
31
# 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