Class: Onceler::Recorder
Instance Attribute Summary collapse
Instance Method Summary
collapse
#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
#tape ⇒ Object
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
123
124
125
126
127
128
|
# File 'lib/onceler/recorder.rb', line 123
def begin_transactions!
Onceler.open_transactions += 1
transactional_connections.each do |connection|
begin_transaction(connection)
end
end
|
#hooks ⇒ Object
83
84
85
86
87
88
|
# File 'lib/onceler/recorder.rb', line 83
def hooks
@hooks ||= {
before: {record: [], reset: []},
after: {record: [], reset: []}
}
end
|
#parent ⇒ Object
79
80
81
|
# File 'lib/onceler/recorder.rb', line 79
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
70
71
|
# 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
run_before_hooks(:record, @tape)
@recordings.each do |recording|
recording.prepare_medium!(@tape)
end
@recordings.each do |recording|
recording.record_onto!(@tape)
end
run_after_hooks(:record, @tape)
@data = @tape.__data
ensure
Onceler.recording = false
end
|
#replay_into!(instance) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/onceler/recorder.rb', line 112
def replay_into!(instance)
@ivars, @retvals = Marshal.load(@data)
@ivars.each do |key, value|
instance.instance_variable_set(key, value)
end
end
|
#reset! ⇒ Object
73
74
75
76
77
|
# File 'lib/onceler/recorder.rb', line 73
def reset!
run_before_hooks(:reset)
rollback_transactions!
run_after_hooks(:reset)
end
|
#rollback_transactions! ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/onceler/recorder.rb', line 130
def rollback_transactions!
transactional_connections.each do |connection|
rollback_transaction(connection)
end
ensure
Onceler.open_transactions -= 1
end
|
#run_after_hooks(scope, context = nil) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/onceler/recorder.rb', line 101
def run_after_hooks(scope, context = nil)
hooks[:after][scope].each do |hook|
context ? context.instance_eval(&hook) : hook.call
end
if parent
parent.run_after_hooks(scope, context)
else
Onceler.configuration.run_hooks(:after, scope, context)
end
end
|
#run_before_hooks(scope, context = nil) ⇒ Object
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/onceler/recorder.rb', line 90
def run_before_hooks(scope, context = nil)
if parent
parent.run_before_hooks(scope, context)
else
Onceler.configuration.run_hooks(:before, scope, context)
end
hooks[:before][scope].each do |hook|
context ? context.instance_eval(&hook) : hook.call
end
end
|
#transactional_connections ⇒ Object
119
120
121
|
# File 'lib/onceler/recorder.rb', line 119
def transactional_connections
@group_class.onceler_connections
end
|