Created
February 6, 2016 10:45
-
-
Save julik/c892cc5da0c5c37d4f5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
class Sqewer::CallRecorder | |
Call = Struct.new(:method_name, :args, :blk) | |
NOOP = ->(*){} | |
def initialize(proxying_to) | |
@obj = proxying_to | |
@mutex = Mutex.new | |
@calls = [] | |
end | |
def respond_to_missing?(m, *a) | |
@obj.respond_to?(m, *a) | |
end | |
def method_missing(m, *a, &blk) | |
blk ||= NOOP | |
@mutex.synchronize { | |
@calls << Call.new(m, a, blk) | |
} | |
end | |
def __replay(on_object) | |
@mutex.synchronize { | |
@calls.each do | call | | |
on_object.public_send(call.method_name, call.args, &call.blk) | |
end | |
@calls.clear | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment