Class: Spy::Instance
- Inherits:
-
Object
- Object
- Spy::Instance
- Defined in:
- lib/spy/instance.rb
Instance Attribute Summary collapse
-
#call_history ⇒ Object
readonly
Returns the value of attribute call_history.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#spied ⇒ Object
readonly
Returns the value of attribute spied.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
Instance Method Summary collapse
- #after(&block) ⇒ Object
- #before(&block) ⇒ Object
- #call_count ⇒ Object
- #called? ⇒ Boolean
-
#initialize(blueprint) ⇒ Instance
constructor
A new instance of Instance.
- #instead(&block) ⇒ Object
- #name ⇒ Object
- #replay_all ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #when(&block) ⇒ Object
-
#wrap(&block) ⇒ Object
Expect block to yield.
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_history ⇒ Object (readonly)
Returns the value of attribute call_history.
11 12 13 |
# File 'lib/spy/instance.rb', line 11 def call_history @call_history end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
11 12 13 |
# File 'lib/spy/instance.rb', line 11 def original @original end |
#spied ⇒ Object (readonly)
Returns the value of attribute spied.
11 12 13 |
# File 'lib/spy/instance.rb', line 11 def spied @spied end |
#strategy ⇒ Object (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_count ⇒ Object
41 42 43 |
# File 'lib/spy/instance.rb', line 41 def call_count @call_history.size end |
#called? ⇒ 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 |
#name ⇒ Object
37 38 39 |
# File 'lib/spy/instance.rb', line 37 def name @original.name end |
#replay_all ⇒ Object
45 46 47 |
# File 'lib/spy/instance.rb', line 45 def replay_all @call_history.map(&:replay) end |
#start ⇒ Object
49 50 51 52 |
# File 'lib/spy/instance.rb', line 49 def start @strategy.apply self end |
#stop ⇒ Object
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 |