class Camcorder::Proxy

Proxy which records all external methods

Attributes

instance[R]
klass[R]
recorder[R]
side_effects[R]

Public Class Methods

methods_with_side_effects(*names) click to toggle source

These methods have “side effects” and will cause subsequent invocations of all methods to be re-recorded.

# File lib/camcorder/proxy.rb, line 65
def self.methods_with_side_effects(*names)
  names.each do |name|
    define_method name do |*args|
      _add_side_effects(args)
      method_missing(name, *args)
    end
  end
end
new(recorder, klass, *args) click to toggle source
# File lib/camcorder/proxy.rb, line 13
def initialize(recorder, klass, *args)
  @klass = klass
  @init_args = args
  @recorder = recorder
  _add_side_effects(@init_args)
end

Public Instance Methods

_add_side_effects(args) click to toggle source
# File lib/camcorder/proxy.rb, line 24
def _add_side_effects(args)
  @side_effects = _hash_with_side_effects(args)
end
_hash_args(args) click to toggle source
# File lib/camcorder/proxy.rb, line 28
def _hash_args(args)
  Digest::MD5.hexdigest(YAML.dump(args))
end
_hash_with_side_effects(args) click to toggle source
# File lib/camcorder/proxy.rb, line 32
def _hash_with_side_effects(args)
  if side_effects
    args = args + [side_effects]
  end
  _hash_args(args)
end
_initialize() click to toggle source
# File lib/camcorder/proxy.rb, line 20
def _initialize
  @instance ||= klass.new(*@init_args)
end
_record(name, args) { || ... } click to toggle source
# File lib/camcorder/proxy.rb, line 39
def _record(name, args, &block)
  hash = _hash_with_side_effects(args)
  key = "#{klass.name}-#{name}-#{hash}"
  recorder.record key do
    yield
  end
rescue PlaybackError => e
  raise ProxyPlaybackError.new(klass, name, args, side_effects)
rescue RecordingError => e
  raise ProxyRecordingError.new(klass, name, args, side_effects)
end
method_missing(name, *args) click to toggle source
# File lib/camcorder/proxy.rb, line 51
def method_missing(name, *args)
  _record(name, args) do
    _initialize.send name, *args
  end
end
respond_to?(name) click to toggle source
Calls superclass method
# File lib/camcorder/proxy.rb, line 57
def respond_to?(name)
  super || _initialize.respond_to?(name)
end