Class: Recorder
Overview
Recorder
Recorder is similar essentially a method probe. It records everthing that happens to it, building an internal parse tree. You can then pass a substitute object and apply the recoding to it. Or you can utilize the parse tree.
The only limitation of Recorder is with special operators, like if, &&, ||, etc. Since they are not true methods they can’t be recorded. (Too bad for Ruby.)
Synopsis
class Z
def name ; 'George' ; end
def age ; 12 ; end
end
z = Z.new
r = Recorder.new
q = proc { |x| (x.name == 'George') & (x.age > 10) }
x = q[r]
x.__call__(z)
produces
true
Instance Method Summary collapse
- #__call__(orig) ⇒ Object
-
#initialize(msg = nil) ⇒ Recorder
constructor
A new instance of Recorder.
- #inspect ⇒ Object
- #method_missing(sym, *args, &blk) ⇒ Object
Constructor Details
#initialize(msg = nil) ⇒ Recorder
Returns a new instance of Recorder.
72 73 74 |
# File 'lib/more/facets/recorder.rb', line 72 def initialize( msg=nil ) @msg = msg end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &blk) ⇒ Object
92 93 94 |
# File 'lib/more/facets/recorder.rb', line 92 def method_missing( sym, *args, &blk ) object_class.new( [ sym, self, *args ] ) end |
Instance Method Details
#__call__(orig) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/more/facets/recorder.rb', line 80 def __call__( orig ) return orig unless @msg sym = @msg[0] args = @msg[1..-1].collect do |a| Recorder === a ? a.__call__(orig) : a end obj = args.shift obj.__send__( sym, *args ) end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/more/facets/recorder.rb', line 76 def inspect "<Recorder #{@msg.inspect}>" end |