Class: MethodCallRecorder
- Inherits:
-
Object
- Object
- MethodCallRecorder
show all
- Defined in:
- lib/method_call_recorder/method_call_recorder.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
61
62
63
64
65
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 61
def method_missing(meth, *args, &blk)
_method_chain << MethodCall.new(meth, *args, &blk)
@on_method_call.call(self) if @on_method_call
self
end
|
Instance Attribute Details
#_method_chain ⇒ Object
32
33
34
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 32
def _method_chain
@_method_chain ||= []
end
|
Instance Method Details
#_empty? ⇒ Boolean
24
25
26
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 24
def _empty?
_method_chain.empty?
end
|
#_first_method ⇒ Object
16
17
18
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 16
def _first_method
_method_chain.first
end
|
#_last_method ⇒ Object
20
21
22
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 20
def _last_method
_method_chain.last
end
|
#_on_method_call(&blk) ⇒ Object
12
13
14
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 12
def _on_method_call(&blk)
@on_method_call = blk
end
|
#_play(object, &blk) ⇒ Object
3
4
5
6
7
8
9
10
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 3
def _play(object, &blk)
i = 0
_method_chain.inject(object) do |obj, method_call|
i += 1
yield(obj, method_call, _method_chain[i]) if block_given?
method_call.call_on(obj)
end
end
|
#_reset! ⇒ Object
43
44
45
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 43
def _reset!
self._method_chain = []
end
|
#_select(meth, args = nil) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 47
def _select(meth, args=nil)
_method_chain.select do |method_call|
selected = (method_call.method == meth)
selected &&= (method_call.args == args) if args
selected
end
end
|
#_to_setter(value) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 36
def _to_setter(value)
new_rec = self.dup
new_rec._method_chain = self._method_chain.dup
new_rec._method_chain[-1] = self._method_chain[-1].to_setter(value)
new_rec
end
|
#to_s ⇒ Object
28
29
30
|
# File 'lib/method_call_recorder/method_call_recorder.rb', line 28
def to_s
_method_chain.inspect
end
|