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



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

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

#[](name) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/onceler/recorder.rb', line 45

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

#[]=(name, block) ⇒ Object



40
41
42
43
# File 'lib/onceler/recorder.rb', line 40

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

#begin_transactions!Object



125
126
127
128
129
130
# File 'lib/onceler/recorder.rb', line 125

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

#hooksObject



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

def hooks
  @hooks ||= {
    before: {record: [], reset: []},
    after:  {record: [], reset: []}
  }
end

#parentObject



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

def parent
  @group_class.parent_onceler
end

#parent_tapeObject



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

def parent_tape
  parent.tape || parent.parent_tape if parent
end

#record!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/onceler/recorder.rb', line 53

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)
  # 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
  run_after_hooks(:record, @tape)
  @data = @tape.__data
ensure
  Onceler.recording = false
end

#replay_into!(instance) ⇒ Object



114
115
116
117
118
119
# File 'lib/onceler/recorder.rb', line 114

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

#reset!Object



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

def reset!
  run_before_hooks(:reset)
  rollback_transactions!
  run_after_hooks(:reset)
end

#rollback_transactions!Object



132
133
134
135
136
137
138
# File 'lib/onceler/recorder.rb', line 132

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/onceler/recorder.rb', line 103

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



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

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_connectionsObject



121
122
123
# File 'lib/onceler/recorder.rb', line 121

def transactional_connections
  @group_class.onceler_connections
end