Class: Tucue::Marker

Inherits:
Object
  • Object
show all
Defined in:
lib/tucue/marker.rb

Overview

Records marks and exports them to CSV / JSON.

Defined Under Namespace

Classes: Mark

Constant Summary collapse

CSV_HEADERS =
%w[timestamp seconds label].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMarker

Returns a new instance of Marker.



25
26
27
# File 'lib/tucue/marker.rb', line 25

def initialize
  @marks = []
end

Instance Attribute Details

#marksObject (readonly)

Returns the value of attribute marks.



29
30
31
# File 'lib/tucue/marker.rb', line 29

def marks
  @marks
end

Instance Method Details

#add(time, label = nil) ⇒ Object

Append a mark at time seconds with an optional label.



32
33
34
35
36
37
# File 'lib/tucue/marker.rb', line 32

def add(time, label = nil)
  label = nil if label.is_a?(String) && label.strip.empty?
  mark = Mark.new(time, label)
  @marks << mark
  mark
end

#empty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/tucue/marker.rb', line 39

def empty?
  @marks.empty?
end

#export_csv(path) ⇒ Object

Write the marks to path as CSV and return the path.



44
45
46
47
48
49
50
51
52
# File 'lib/tucue/marker.rb', line 44

def export_csv(path)
  CSV.open(path, "w") do |csv|
    csv << CSV_HEADERS
    @marks.each do |mark|
      csv << [mark.timestamp, mark.time.to_f.round(3), mark.label]
    end
  end
  path
end

#export_json(path) ⇒ Object

Write the marks to path as JSON and return the path.



55
56
57
58
# File 'lib/tucue/marker.rb', line 55

def export_json(path)
  File.write(path, JSON.pretty_generate(@marks.map(&:to_h)))
  path
end