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.



26
27
28
29
30
# File 'lib/onceler/recorder.rb', line 26

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

Instance Attribute Details

#tapeObject

Returns the value of attribute tape.



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

def tape
  @tape
end

Instance Method Details

#<<(block) ⇒ Object



32
33
34
# File 'lib/onceler/recorder.rb', line 32

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

#[](name) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/onceler/recorder.rb', line 41

def [](name)
  if @retvals && @retvals.key?(name)
    @retvals[name]
  elsif parent
    parent[name]
  end
end

#[]=(name, block) ⇒ Object



36
37
38
39
# File 'lib/onceler/recorder.rb', line 36

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

#begin_transactions!Object



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

def begin_transactions!
  Onceler.open_transactions += 1
  transaction_classes.each do |klass|
    begin_transaction(klass.connection)
  end
end

#parentObject



92
93
94
# File 'lib/onceler/recorder.rb', line 92

def parent
  @group_class.parent_onceler
end

#reconsitute_data!Object



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

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

#record!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/onceler/recorder.rb', line 49

def record!
  Onceler.recording = true
  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
ensure
  Onceler.recording = false
end

#reidentify!(hash, identity_map) ⇒ Object



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

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



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

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

#reset!Object



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

def reset!
  rollback_transactions!
end

#rollback_transactions!Object



117
118
119
120
121
122
123
# File 'lib/onceler/recorder.rb', line 117

def rollback_transactions!
  transaction_classes.each do |klass|
    rollback_transaction(klass.connection)
  end
ensure
  Onceler.open_transactions -= 1
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