class Onceler::Recorder

Attributes

tape[RW]

Public Class Methods

new(group_class) click to toggle source
# File lib/onceler/recorder.rb, line 26
def initialize(group_class)
  @group_class = group_class
  @recordings = []
  @named_recordings = []
  @arounds = []
end

Public Instance Methods

<<(block) click to toggle source
# File lib/onceler/recorder.rb, line 37
def <<(block)
  @recordings << Recording.new(block)
end
[](name) click to toggle source
# File lib/onceler/recorder.rb, line 46
def [](name)
  if @retvals && @retvals.key?(name)
    @retvals[name]
  elsif parent
    parent[name]
  end
end
[]=(name, block) click to toggle source
# File lib/onceler/recorder.rb, line 41
def []=(name, block)
  @named_recordings << name
  @recordings << NamedRecording.new(block, name)
end
add_around(block) click to toggle source
# File lib/onceler/recorder.rb, line 54
def add_around(block)
  @arounds.unshift(block)
end
arounds() click to toggle source
# File lib/onceler/recorder.rb, line 58
def arounds
  (parent ? parent.arounds : []) + @arounds
end
begin_transactions!() click to toggle source
# 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() click to toggle source
# File lib/onceler/recorder.rb, line 103
def hooks
  @hooks ||= {
    before: {record: [], reset: []},
    after:  {record: [], reset: []}
  }
end
parent() click to toggle source
# File lib/onceler/recorder.rb, line 99
def parent
  @group_class.parent_onceler
end
parent_tape() click to toggle source
# File lib/onceler/recorder.rb, line 33
def parent_tape
  parent.tape || parent.parent_tape if parent
end
record!() click to toggle source
# 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)
  # 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) click to toggle source
# 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!() click to toggle source
# File lib/onceler/recorder.rb, line 93
def reset!
  run_before_hooks(:reset)
  rollback_transactions!
  run_after_hooks(:reset)
end
rollback_transactions!() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/onceler/recorder.rb, line 139
def transactional_connections
  @group_class.onceler_connections
end