Class: Tucue::Marker
- Inherits:
-
Object
- Object
- Tucue::Marker
- 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
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
Instance Method Summary collapse
-
#add(time, label = nil) ⇒ Object
Append a mark at
timeseconds with an optionallabel. - #empty? ⇒ Boolean
-
#export_csv(path) ⇒ Object
Write the marks to
pathas CSV and return the path. -
#export_json(path) ⇒ Object
Write the marks to
pathas JSON and return the path. -
#initialize ⇒ Marker
constructor
A new instance of Marker.
Constructor Details
#initialize ⇒ Marker
Returns a new instance of Marker.
25 26 27 |
# File 'lib/tucue/marker.rb', line 25 def initialize @marks = [] end |
Instance Attribute Details
#marks ⇒ Object (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
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., 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 |