class Impersonator::Recording

A recording is responsible for saving interactions at record time, and replaying them at replay time.

A recording is always in one of two states.

The state objects are responsible of dealing with the recording logic, which happens in 3 moments:

@see RecordMode @see ReplayMode

Attributes

current_mode[R]
label[R]

Public Class Methods

new(label, disabled: false, recordings_path:) click to toggle source

@param [String] label @param [Boolean] disabled `true` for always working in record mode. `false` by default @param [String] the path to save recordings to

# File lib/impersonator/recording.rb, line 29
def initialize(label, disabled: false, recordings_path:)
  @label = label
  @recordings_path = recordings_path
  @disabled = disabled

  initialize_current_mode
end

Public Instance Methods

finish() click to toggle source

Finish a record/replay session.

# File lib/impersonator/recording.rb, line 56
def finish
  logger.debug "Recording #{label} finished"
  current_mode.finish
end
invoke(impersonated_object, method, args) click to toggle source

Handles the invocation of a given method on the impersonated object

It will either record the interaction or replay it dependening on if there is a recording available or not

@param [Object, Double] impersonated_object @param [MethodInvocation] method @param [Array<Object>] args

# File lib/impersonator/recording.rb, line 51
def invoke(impersonated_object, method, args)
  current_mode.invoke(impersonated_object, method, args)
end
record_mode?() click to toggle source

Return whether it is currently at record mode

@return [Boolean]

# File lib/impersonator/recording.rb, line 71
def record_mode?
  !replay_mode?
end
replay_mode?() click to toggle source

Return whether it is currently at replay mode

@return [Boolean]

# File lib/impersonator/recording.rb, line 64
def replay_mode?
  @current_mode == replay_mode
end
start() click to toggle source

Start a recording/replay session

# File lib/impersonator/recording.rb, line 38
def start
  logger.debug "Starting recording #{label}..."
  current_mode.start
end

Private Instance Methods

can_replay?() click to toggle source
# File lib/impersonator/recording.rb, line 87
def can_replay?
  !@disabled && File.exist?(recording_path)
end
initialize_current_mode() click to toggle source
# File lib/impersonator/recording.rb, line 79
def initialize_current_mode
  @current_mode = if can_replay?
                    replay_mode
                  else
                    record_mode
                  end
end
label_as_file_name() click to toggle source
# File lib/impersonator/recording.rb, line 103
def label_as_file_name
  label.downcase.gsub(/[\(\)\s \#:]/, '-').gsub(/[\-]+/, '-').gsub(/(^-)|(-$)/, '')
end
record_mode() click to toggle source
# File lib/impersonator/recording.rb, line 91
def record_mode
  @record_mode ||= RecordMode.new(recording_path)
end
recording_path() click to toggle source
# File lib/impersonator/recording.rb, line 99
def recording_path
  File.join(@recordings_path, "#{label_as_file_name}.yml")
end
replay_mode() click to toggle source
# File lib/impersonator/recording.rb, line 95
def replay_mode
  @replay_mode ||= ReplayMode.new(recording_path)
end