Skip to content

Instantly share code, notes, and snippets.

@julik
Created February 6, 2016 10:45
Show Gist options
  • Save julik/c892cc5da0c5c37d4f5c to your computer and use it in GitHub Desktop.
Save julik/c892cc5da0c5c37d4f5c to your computer and use it in GitHub Desktop.
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