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



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

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

#parentObject



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

def parent
  @group_class.parent_onceler
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

#replay_into!(instance) ⇒ Object



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

def replay_into!(instance)
  @ivars, @retvals = Marshal.load(@data)
  @ivars.each do |key, value|
    instance.instance_variable_set(key, value)
  end
end

#reset!Object



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

def reset!
  rollback_transactions!
end

#rollback_transactions!Object



99
100
101
102
103
104
105
# File 'lib/onceler/recorder.rb', line 99

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)



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

def transaction_classes
  [ActiveRecord::Base]
end