Class: Backspin::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/backspin/record.rb

Constant Summary collapse

FORMAT_VERSION =
"2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Record

Returns a new instance of Record.



38
39
40
41
42
43
# File 'lib/backspin/record.rb', line 38

def initialize(path)
  @path = path
  @commands = []
  @first_recorded_at = nil
  @playback_index = 0
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



10
11
12
# File 'lib/backspin/record.rb', line 10

def commands
  @commands
end

#first_recorded_atObject (readonly)

Returns the value of attribute first_recorded_at.



10
11
12
# File 'lib/backspin/record.rb', line 10

def first_recorded_at
  @first_recorded_at
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/backspin/record.rb', line 10

def path
  @path
end

Class Method Details

.build_record_path(name) ⇒ Object



26
27
28
29
30
31
# File 'lib/backspin/record.rb', line 26

def self.build_record_path(name)
  backspin_dir = Backspin.configuration.backspin_dir
  backspin_dir.mkpath

  File.join(backspin_dir, "#{name}.yml")
end

.create(name) ⇒ Object



33
34
35
36
# File 'lib/backspin/record.rb', line 33

def self.create(name)
  path = build_record_path(name)
  new(path)
end

.load_from_file(path) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/backspin/record.rb', line 18

def self.load_from_file(path)
  raise Backspin::RecordNotFoundError unless File.exist?(path)

  record = new(path)
  record.load_from_file
  record
end

.load_or_create(path) ⇒ Object



12
13
14
15
16
# File 'lib/backspin/record.rb', line 12

def self.load_or_create(path)
  record = new(path)
  record.load_from_file if File.exist?(path)
  record
end

Instance Method Details

#add_command(command) ⇒ Object



45
46
47
48
49
# File 'lib/backspin/record.rb', line 45

def add_command(command)
  @commands << command
  @first_recorded_at ||= command.recorded_at
  self
end

#clearObject



88
89
90
91
# File 'lib/backspin/record.rb', line 88

def clear
  @commands = []
  @playback_index = 0
end

#empty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/backspin/record.rb', line 72

def empty?
  @commands.empty?
end

#exists?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/backspin/record.rb', line 68

def exists?
  File.exist?(@path)
end

#load_from_fileObject

private



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/backspin/record.rb', line 95

def load_from_file
  data = YAML.load_file(@path.to_s)

  unless data.is_a?(Hash) && data["format_version"] == "2.0"
    raise RecordFormatError, "Invalid record format: expected format version 2.0"
  end

  @first_recorded_at = data["first_recorded_at"]
  @commands = data["commands"].map { |command_data| Command.from_h(command_data) }
rescue Psych::SyntaxError => e
  raise RecordFormatError, "Invalid record format: #{e.message}"
end

#next_commandObject



80
81
82
83
84
85
86
# File 'lib/backspin/record.rb', line 80

def next_command
  raise NoMoreRecordingsError, "No more recordings available for replay" if @playback_index >= @commands.size

  command = @commands[@playback_index]
  @playback_index += 1
  command
end

#reloadObject



61
62
63
64
65
66
# File 'lib/backspin/record.rb', line 61

def reload
  @commands = []
  @playback_index = 0
  load_from_file if File.exist?(@path)
  @playback_index = 0 # Reset again after loading to ensure it's at 0
end

#save(filter: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/backspin/record.rb', line 51

def save(filter: nil)
  FileUtils.mkdir_p(File.dirname(@path))
  record_data = {
    "first_recorded_at" => @first_recorded_at,
    "format_version" => FORMAT_VERSION,
    "commands" => @commands.map { |cmd| cmd.to_h(filter: filter) }
  }
  File.write(@path, record_data.to_yaml)
end

#sizeObject



76
77
78
# File 'lib/backspin/record.rb', line 76

def size
  @commands.size
end