Class: Camcorder::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/camcorder/proxy.rb

Overview

Proxy which records all external methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recorder, klass, *args) ⇒ Proxy

Returns a new instance of Proxy.



13
14
15
16
17
18
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



51
52
53
54
55
# File 'lib/camcorder/proxy.rb', line 51

def method_missing(name, *args)
  _record(name, args) do
    _initialize.send name, *args
  end
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



9
10
11
# File 'lib/camcorder/proxy.rb', line 9

def instance
  @instance
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/camcorder/proxy.rb', line 8

def klass
  @klass
end

#recorderObject (readonly)

Returns the value of attribute recorder.



10
11
12
# File 'lib/camcorder/proxy.rb', line 10

def recorder
  @recorder
end

#side_effectsObject (readonly)

Returns the value of attribute side_effects.



11
12
13
# File 'lib/camcorder/proxy.rb', line 11

def side_effects
  @side_effects
end

Class Method Details

.methods_with_side_effects(*names) ⇒ Object

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



65
66
67
68
69
70
71
72
# 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

Instance Method Details

#_add_side_effects(args) ⇒ Object



24
25
26
# File 'lib/camcorder/proxy.rb', line 24

def _add_side_effects(args)
  @side_effects = _hash_with_side_effects(args)
end

#_hash_args(args) ⇒ Object



28
29
30
# File 'lib/camcorder/proxy.rb', line 28

def _hash_args(args)
  Digest::MD5.hexdigest(YAML.dump(args))
end

#_hash_with_side_effects(args) ⇒ Object



32
33
34
35
36
37
# 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

#_initializeObject



20
21
22
# File 'lib/camcorder/proxy.rb', line 20

def _initialize
  @instance ||= klass.new(*@init_args)
end

#_record(name, args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# 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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/camcorder/proxy.rb', line 57

def respond_to?(name)
  super || _initialize.respond_to?(name)
end