Class: Backspin::Record
- Inherits:
-
Object
- Object
- Backspin::Record
- Defined in:
- lib/backspin/record.rb
Constant Summary collapse
- FORMAT_VERSION =
"2.0"
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#first_recorded_at ⇒ Object
readonly
Returns the value of attribute first_recorded_at.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .build_record_path(name) ⇒ Object
- .create(name) ⇒ Object
- .load_from_file(path) ⇒ Object
- .load_or_create(path) ⇒ Object
Instance Method Summary collapse
- #add_command(command) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #exists? ⇒ Boolean
-
#initialize(path) ⇒ Record
constructor
A new instance of Record.
-
#load_from_file ⇒ Object
private.
- #next_command ⇒ Object
- #reload ⇒ Object
- #save(filter: nil) ⇒ Object
- #size ⇒ Object
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
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
10 11 12 |
# File 'lib/backspin/record.rb', line 10 def commands @commands end |
#first_recorded_at ⇒ Object (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 |
#path ⇒ Object (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 |
#clear ⇒ Object
88 89 90 91 |
# File 'lib/backspin/record.rb', line 88 def clear @commands = [] @playback_index = 0 end |
#empty? ⇒ Boolean
72 73 74 |
# File 'lib/backspin/record.rb', line 72 def empty? @commands.empty? end |
#exists? ⇒ Boolean
68 69 70 |
# File 'lib/backspin/record.rb', line 68 def exists? File.exist?(@path) end |
#load_from_file ⇒ Object
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.}" end |
#next_command ⇒ Object
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 |
#reload ⇒ Object
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 |
#size ⇒ Object
76 77 78 |
# File 'lib/backspin/record.rb', line 76 def size @commands.size end |