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
31
|
# File 'lib/onceler/recorder.rb', line 26
def initialize(group_class)
@group_class = group_class
@recordings = []
@named_recordings = []
@arounds = []
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
37
38
39
|
# File 'lib/onceler/recorder.rb', line 37
def <<(block)
@recordings << Recording.new(block)
end
|
#[](name) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/onceler/recorder.rb', line 46
def [](name)
if @retvals && @retvals.key?(name)
@retvals[name]
elsif parent
parent[name]
end
end
|
#[]=(name, block) ⇒ Object
41
42
43
44
|
# File 'lib/onceler/recorder.rb', line 41
def []=(name, block)
@named_recordings << name
@recordings << NamedRecording.new(block, name)
end
|
#add_around(block) ⇒ Object
54
55
56
|
# File 'lib/onceler/recorder.rb', line 54
def add_around(block)
@arounds.unshift(block)
end
|
#arounds ⇒ Object
58
59
60
|
# File 'lib/onceler/recorder.rb', line 58
def arounds
(parent ? parent.arounds : []) + @arounds
end
|
#begin_transactions! ⇒ Object
143
144
145
146
147
148
|
# File 'lib/onceler/recorder.rb', line 143
def begin_transactions!
Onceler.open_transactions += 1
transactional_connections.each do |connection|
begin_transaction(connection)
end
end
|
#hooks ⇒ Object
103
104
105
106
107
108
|
# File 'lib/onceler/recorder.rb', line 103
def hooks
@hooks ||= {
before: {record: [], reset: []},
after: {record: [], reset: []}
}
end
|
#parent ⇒ Object
99
100
101
|
# File 'lib/onceler/recorder.rb', line 99
def parent
@group_class.parent_onceler
end
|
#parent_tape ⇒ Object
33
34
35
|
# File 'lib/onceler/recorder.rb', line 33
def parent_tape
parent.tape || parent.parent_tape if parent
end
|
#record! ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/onceler/recorder.rb', line 62
def record!
Onceler.recording = true
begin_transactions!
@tape = @group_class.new
@tape.send :extend, Recordable
@tape.copy_from(parent_tape) if parent_tape
run_before_hooks(:record, @tape)
@recordings.each do |recording|
recording.prepare_medium!(@tape)
end
stack = -> do
@recordings.each do |recording|
recording.record_onto!(@tape)
end
end
arounds.inject(stack) do |old_stack, hook|
-> { @tape.instance_exec(old_stack, &hook) }
end.call
run_after_hooks(:record, @tape)
@data = @tape.__data
ensure
Onceler.recording = false
end
|
#replay_into!(instance) ⇒ Object
132
133
134
135
136
137
|
# File 'lib/onceler/recorder.rb', line 132
def replay_into!(instance)
@ivars, @retvals = Marshal.load(@data)
@ivars.each do |key, value|
instance.instance_variable_set(key, value)
end
end
|
#reset! ⇒ Object
93
94
95
96
97
|
# File 'lib/onceler/recorder.rb', line 93
def reset!
run_before_hooks(:reset)
rollback_transactions!
run_after_hooks(:reset)
end
|
#rollback_transactions! ⇒ Object
150
151
152
153
154
155
156
|
# File 'lib/onceler/recorder.rb', line 150
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
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/onceler/recorder.rb', line 121
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
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/onceler/recorder.rb', line 110
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
139
140
141
|
# File 'lib/onceler/recorder.rb', line 139
def transactional_connections
@group_class.onceler_connections
end
|