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(group_class) ⇒ Recorder

Returns a new instance of Recorder.



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

def initialize(group_class)
  @group_class = group_class
  @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



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

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

#reconsitute_data!Object



52
53
54
55
56
57
# File 'lib/onceler/recorder.rb', line 52

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
44
45
46
# File 'lib/onceler/recorder.rb', line 29

def record!
  begin_transactions!
  @tape = @group_class.new
  @tape.send :extend, Recordable
  if parent = @group_class.parent_onceler
    @tape.copy_from(parent.tape)
  end

  # 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



59
60
61
62
63
64
65
66
67
# File 'lib/onceler/recorder.rb', line 59

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



69
70
71
72
73
74
# File 'lib/onceler/recorder.rb', line 69

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

#reset!Object



48
49
50
# File 'lib/onceler/recorder.rb', line 48

def reset!
  rollback_transactions!
end

#rollback_transactions!Object



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

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)



78
79
80
# File 'lib/onceler/recorder.rb', line 78

def transaction_classes
  [ActiveRecord::Base]
end