Module: Camcorder

Defined in:
lib/camcorder.rb,
lib/camcorder/proxy.rb,
lib/camcorder/errors.rb,
lib/camcorder/version.rb,
lib/camcorder/recorder.rb,
lib/camcorder/recording.rb,
lib/camcorder/configuration.rb

Overview

Provides similar functionality as the VCR gem but works on arbitrary objects. This is useful for things like Net::IMAP and Net::SMTP which cannot be captured by VCR.

Defined Under Namespace

Classes: Configuration, Error, PlaybackError, Proxy, ProxyPlaybackError, ProxyRecordingError, Recorder, Recording, RecordingError

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.configObject



22
23
24
# File 'lib/camcorder.rb', line 22

def self.config
  @config ||= Configuration.new
end

.default_recorderObject



14
15
16
# File 'lib/camcorder.rb', line 14

def self.default_recorder
  @default_recorder
end

.default_recorder=(value) ⇒ Object



18
19
20
# File 'lib/camcorder.rb', line 18

def self.default_recorder=(value)
  @default_recorder = value
end

.deintercept_constructor(klass) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/camcorder.rb', line 67

def self.deintercept_constructor(klass)
  klass.class_eval do
    class << self
      alias_method :new, :_new
      remove_method :_new
    end
  end
end

.intercept_constructor(klass, recorder = nil, &block) ⇒ Object

Rewrites the ‘new` method on the passed in class to always return proxies.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/camcorder.rb', line 48

def self.intercept_constructor(klass, recorder=nil, &block)
  proxy_class = self.proxy_class(klass, recorder) do
    def _initialize
      @instance ||= klass._new(*@init_args)
    end
    self.class_eval &block if block
  end
  
  klass.class_eval do
    @proxy_class = proxy_class
    class << self
      alias_method :_new, :new
      def new(*args)
        @proxy_class.new(*args)
      end
    end
  end
end

.proxy_class(klass, recorder = nil, &block) ⇒ Object

Creates a proxy subclass for the particular class



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/camcorder.rb', line 29

def self.proxy_class(klass, recorder=nil, &block)
  Class.new(Proxy) do
    class << self
      attr_reader :klass
      attr_reader :recorder
    end
    @klass = klass
    @recorder = recorder
    def initialize(*args)
      recorder = self.class.recorder || Camcorder.default_recorder
      super(recorder, self.class.klass, *args)
    end
    self.class_eval &block if block
  end
end