Class: Onceler::Recorder

Inherits:
Object
  • Object
show all
Includes:
Transactions
Defined in:
lib/onceler/recorder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transactions

#begin_transaction, #rollback_transaction

Constructor Details

#initialize(parent) ⇒ Recorder

Returns a new instance of Recorder.



10
11
12
13
14
# File 'lib/onceler/recorder.rb', line 10

def initialize(parent)
  @parent = parent
  @recordings = []
  @named_recordings = []
end

Instance Attribute Details

#helper_proxyObject

Returns the value of attribute helper_proxy.



8
9
10
# File 'lib/onceler/recorder.rb', line 8

def helper_proxy
  @helper_proxy
end

#tapeObject

Returns the value of attribute tape.



8
9
10
# File 'lib/onceler/recorder.rb', line 8

def tape
  @tape
end

Instance Method Details

#<<(block) ⇒ Object



16
17
18
# File 'lib/onceler/recorder.rb', line 16

def <<(block)
  @recordings << Recording.new(block)
end

#[](name) ⇒ Object



25
26
27
# File 'lib/onceler/recorder.rb', line 25

def [](name)
  @retvals[name]
end

#[]=(name, block) ⇒ Object



20
21
22
23
# File 'lib/onceler/recorder.rb', line 20

def []=(name, block)
  @named_recordings << name
  @recordings << NamedRecording.new(block, name)
end

#begin_transactions!Object



110
111
112
113
114
# File 'lib/onceler/recorder.rb', line 110

def begin_transactions!
  transaction_classes.each do |klass|
    begin_transaction(klass.connection)
  end
end

#helper_methodsObject



63
64
65
# File 'lib/onceler/recorder.rb', line 63

def helper_methods
  @helper_methods ||= {}
end

#mixinsObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/onceler/recorder.rb', line 67

def mixins
  mixins = (@parent ? @parent.mixins : Onceler.configuration.modules).dup
  if methods = @helper_methods
    mixin = Module.new do
      methods.each do |key, method|
        define_method(key, &method)
      end
    end
    mixins.push mixin
  end
  mixins
end

#proxy_recordable_methods!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/onceler/recorder.rb', line 49

def proxy_recordable_methods!
  # the proxy is used to run non-recordable methods that may be called
  # by ones are recording. since the former could in turn call more of
  # the latter, we need to proxy the other way too
  return unless helper_proxy
  methods = @named_recordings
  reverse_proxy = @tape
  helper_proxy.instance_eval do
    methods.each do |method|
      define_singleton_method(method) { reverse_proxy.send(method) }
    end
  end
end

#reconsitute_data!Object



80
81
82
83
84
85
# File 'lib/onceler/recorder.rb', line 80

def reconsitute_data!
  @ivars, @retvals = Marshal.load(@data)
  identity_map = {}
  reidentify!(@ivars, identity_map)
  reidentify!(@retvals, identity_map)
end

#record!Object



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

def record!
  begin_transactions!
  @tape = @parent ? @parent.tape.copy(mixins) : BlankTape.new(mixins)
  proxy_recordable_methods!

  # we don't know the order named recordings will be called (or if
  # they'll call each other), so prep everything first
  @recordings.each do |recording|
    recording.prepare_medium!(@tape)
  end
  @recordings.each do |recording|
    recording.record_onto!(@tape)
  end
  @data = @tape.__data
end

#reidentify!(hash, identity_map) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/onceler/recorder.rb', line 87

def reidentify!(hash, identity_map)
  hash.each do |key, value|
    if identity_map.key?(value)
      hash[key] = identity_map[value]
    else
      identity_map[value] = value
    end
  end
end

#replay_into!(instance) ⇒ Object



97
98
99
100
101
102
# File 'lib/onceler/recorder.rb', line 97

def replay_into!(instance)
  reconsitute_data!
  @ivars.each do |key, value|
    instance.instance_variable_set(key, value)
  end
end

#reset!Object



45
46
47
# File 'lib/onceler/recorder.rb', line 45

def reset!
  rollback_transactions!
end

#rollback_transactions!Object



116
117
118
119
120
# File 'lib/onceler/recorder.rb', line 116

def rollback_transactions!
  transaction_classes.each do |klass|
    rollback_transaction(klass.connection)
  end
end

#transaction_classesObject

TODO: configurable transaction fu (say, if you have multiple conns)



106
107
108
# File 'lib/onceler/recorder.rb', line 106

def transaction_classes
  [ActiveRecord::Base]
end