class Captive::Cue
Constants
- ALIGNMENT
Text Properties supported
- COLOR
- POSITION
- TEXT_PROPERTIES
List of Text Properties
Attributes
end_time[R]
properties[RW]
start_time[R]
text[RW]
Public Class Methods
from_json(json:, mapping: {})
click to toggle source
# File lib/captive/cue.rb, line 26 def self.from_json(json:, mapping: {}) schema = {} %i[text! start_time! end_time! properties].each do |field| field_name = field.to_s.delete('!') schema[field] = mapping[field_name] || mapping[field_name.to_sym] || field_name.to_sym end data = {} schema.each do |mask, mapper| key = mask[-1] == '!' ? mask.to_s[0...-1].to_sym : mask if key.to_s != mask.to_s && !(json.key?(mapper.to_s) || json.key?(mapper.to_sym)) raise InvalidJsonInput, "Cue missing field: #{mapper}" end data[key] = json[mapper.to_s] || json[mapper.to_sym] end new(**data) end
new(text: nil, start_time: nil, end_time: nil, properties: {})
click to toggle source
Creates a new Cue
class denoting a subtitle.
# File lib/captive/cue.rb, line 19 def initialize(text: nil, start_time: nil, end_time: nil, properties: {}) self.text = text self.start_time = start_time self.end_time = end_time self.properties = properties || {} end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/captive/cue.rb, line 83 def <=>(other) start_time <=> other.start_time end
add_text(text)
click to toggle source
Adds text. If text is already present, new-line is added before text.
# File lib/captive/cue.rb, line 75 def add_text(text) if self.text.nil? self.text = text else self.text += "\n" + text end end
as_json(**args)
click to toggle source
# File lib/captive/cue.rb, line 87 def as_json(**args) options = args.delete(:options) || {} format = options['format'] || options[:format] || {} mapping = (options['mapping'] || options[:mapping] || {}).map { |k, v| [k.to_s, v.to_s] }.to_h obj = { mapping['start_time'] || 'start_time' => format[:time] == :timecode ? milliseconds_to_timecode(start_time) : start_time, mapping['end_time'] || 'end_time' => format[:time] == :timecode ? milliseconds_to_timecode(end_time) : end_time, mapping['text'] || 'text' => text, mapping['properties'] || 'properties' => properties, } obj.respond_to?(:as_json) ? obj.as_json(**args) : obj end
duration()
click to toggle source
# File lib/captive/cue.rb, line 70 def duration end_time - start_time end
end_time=(time)
click to toggle source
# File lib/captive/cue.rb, line 48 def end_time=(time) set_time(:end_time, time) end
set_times(start_time:, end_time:)
click to toggle source
# File lib/captive/cue.rb, line 52 def set_times(start_time:, end_time:) self.start_time = start_time self.end_time = end_time end
start_time=(time)
click to toggle source
# File lib/captive/cue.rb, line 44 def start_time=(time) set_time(:start_time, time) end
Private Instance Methods
set_time(field, time)
click to toggle source
# File lib/captive/cue.rb, line 102 def set_time(field, time) return if time.nil? if time.is_a?(Integer) instance_variable_set("@#{field}", time) elsif TIMECODE_REGEX.match(time) instance_variable_set("@#{field}", timecode_to_milliseconds(time)) else raise InvalidInput, "Input for #{field} should be an integer denoting milliseconds or a valid timecode." end end