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
31
# File 'lib/onceler/recorder.rb', line 26

def initialize(group_class)
  @group_class = group_class
  @recordings = []
  @named_recordings = []
  @arounds = []
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



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

#begin_transactions!Object



139
140
141
142
143
144
# File 'lib/onceler/recorder.rb', line 139

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

#hooksObject



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

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

#parentObject



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

def parent
  @group_class.parent_onceler
end

#parent_tapeObject



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

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

#record!Object



58
59
60
61
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
# File 'lib/onceler/recorder.rb', line 58

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

  # wrap the before in a lambda
  stack = -> do
    @recordings.each do |recording|
      recording.record_onto!(@tape)
    end
  end
  # and then stack each around block on top
  @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



128
129
130
131
132
133
# File 'lib/onceler/recorder.rb', line 128

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

#reset!Object



89
90
91
92
93
# File 'lib/onceler/recorder.rb', line 89

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

#rollback_transactions!Object



146
147
148
149
150
151
152
# File 'lib/onceler/recorder.rb', line 146

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



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

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



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

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



135
136
137
# File 'lib/onceler/recorder.rb', line 135

def transactional_connections
  @group_class.onceler_connections
end