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

#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

#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