Class: Spy::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/spy/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blueprint) ⇒ Instance

Returns a new instance of Instance.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spy/instance.rb', line 13

def initialize(blueprint)
  original =
    case blueprint.type
    when :dynamic_delegation
      FakeMethod.new(blueprint.msg) { |*args, &block| blueprint.target.method_missing(blueprint.msg, *args, &block) }
    when :instance_method
      blueprint.target.instance_method(blueprint.msg)
    else
      blueprint.target.method(blueprint.msg)
    end

  @original = original
  @spied = blueprint.target
  @strategy = choose_strategy(blueprint)
  @call_history = []

  @internal = {}
  @internal[:conditional_filters] = []
  @internal[:before_callbacks] = []
  @internal[:after_callbacks]= []
  @internal[:around_procs] = []
  @internal[:instead]= nil
end

Instance Attribute Details

#call_historyObject (readonly)

Returns the value of attribute call_history.



11
12
13
# File 'lib/spy/instance.rb', line 11

def call_history
  @call_history
end

#originalObject (readonly)

Returns the value of attribute original.



11
12
13
# File 'lib/spy/instance.rb', line 11

def original
  @original
end

#spiedObject (readonly)

Returns the value of attribute spied.



11
12
13
# File 'lib/spy/instance.rb', line 11

def spied
  @spied
end

#strategyObject (readonly)

Returns the value of attribute strategy.



11
12
13
# File 'lib/spy/instance.rb', line 11

def strategy
  @strategy
end

Instance Method Details

#after(&block) ⇒ Object



76
77
78
79
# File 'lib/spy/instance.rb', line 76

def after(&block)
  @internal[:after_callbacks] << block
  self
end

#apply(method_call) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/spy/instance.rb', line 100

def apply(method_call)
  return method_call.call_original unless passes_all_conditions?(method_call)

  result = nil
  runner =
    if @internal[:instead]
      proc do
        @call_history << method_call
        result = @internal[:instead].call(method_call)
      end
    else
      proc do
        @call_history << method_call
        result = method_call.call_original(true)
      end
    end

  if @internal[:around_procs].any?
    runner = @internal[:around_procs].reduce(runner) do |p, wrapper|
      proc { wrapper[method_call, &p] }
    end
  end

  run_before_callbacks(method_call)

  runner.call

  run_after_callbacks(method_call)

  result
end

#before(&block) ⇒ Object



71
72
73
74
# File 'lib/spy/instance.rb', line 71

def before(&block)
  @internal[:before_callbacks] << block
  self
end

#call_countObject



41
42
43
# File 'lib/spy/instance.rb', line 41

def call_count
  @call_history.size
end

#call_original(*args) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/spy/instance.rb', line 91

def call_original(*args)
  if original.is_a?(UnboundMethod)
    call_original_unbound_method(*args)
  else
    call_original_method(*args)
  end
end

#called?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/spy/instance.rb', line 86

def called?
  @call_history.any?
end

#instead(&block) ⇒ Object



81
82
83
84
# File 'lib/spy/instance.rb', line 81

def instead(&block)
  @internal[:instead] = block
  self
end

#nameObject



37
38
39
# File 'lib/spy/instance.rb', line 37

def name
  @original.name
end

#replay_allObject



45
46
47
# File 'lib/spy/instance.rb', line 45

def replay_all
  @call_history.map(&:replay)
end

#startObject



49
50
51
52
# File 'lib/spy/instance.rb', line 49

def start
  @strategy.apply
  self
end

#stopObject



54
55
56
57
# File 'lib/spy/instance.rb', line 54

def stop
  @strategy.undo
  self
end

#when(&block) ⇒ Object



59
60
61
62
# File 'lib/spy/instance.rb', line 59

def when(&block)
  @internal[:conditional_filters] << block
  self
end

#wrap(&block) ⇒ Object

Expect block to yield. Call the rest of the chain when it does



66
67
68
69
# File 'lib/spy/instance.rb', line 66

def wrap(&block)
  @internal[:around_procs] << block
  self
end