Class: Captive::Cue

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/captive/cue.rb

Constant Summary collapse

ALIGNMENT =

Text Properties supported

'alignment'
COLOR =
'color'
POSITION =
'position'
TEXT_PROPERTIES =

List of Text Properties

[ALIGNMENT, COLOR, POSITION].freeze

Constants included from Util

Util::TIMECODE_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#hours_to_seconds, #milliseconds_to_timecode, #minutes_to_seconds, #seconds_to_milliseconds, #timecode_to_milliseconds

Constructor Details

#initialize(text: nil, start_time: nil, end_time: nil, cue_number: nil, properties: {}) ⇒ Cue

Creates a new Cue class denoting a subtitle.



19
20
21
22
23
24
25
# File 'lib/captive/cue.rb', line 19

def initialize(text: nil, start_time: nil, end_time: nil, cue_number: nil, properties: {})
  self.text = text
  self.start_time = start_time
  self.end_time = end_time
  self.number = cue_number
  self.properties = properties || {}
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



16
17
18
# File 'lib/captive/cue.rb', line 16

def end_time
  @end_time
end

#numberObject

Returns the value of attribute number.



15
16
17
# File 'lib/captive/cue.rb', line 15

def number
  @number
end

#propertiesObject

Returns the value of attribute properties.



15
16
17
# File 'lib/captive/cue.rb', line 15

def properties
  @properties
end

#start_timeObject

Returns the value of attribute start_time.



16
17
18
# File 'lib/captive/cue.rb', line 16

def start_time
  @start_time
end

#textObject

Returns the value of attribute text.



15
16
17
# File 'lib/captive/cue.rb', line 15

def text
  @text
end

Class Method Details

.from_json(json:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/captive/cue.rb', line 27

def self.from_json(json:)
  schema = {
    text!: :text,
    start_time!: :start_time,
    end_time!: :end_time,
    number!: :cue_number,
    properties: :properties,
  }
  data = {}
  schema.each do |mask, value|
    key = mask[-1] == '!' ? mask.to_s[0...-1] : mask.to_s
    raise InvalidJsonInput, "Cue missing field: #{key}" if key.to_s != mask.to_s && !json.key?(key)

    data[value] = json[key]
  end

  new(**data)
end

Instance Method Details

#<=>(other) ⇒ Object



85
86
87
# File 'lib/captive/cue.rb', line 85

def <=>(other)
  start_time <=> other.start_time
end

#add_text(text) ⇒ Object

Adds text. If text is already present, new-line is added before text.



77
78
79
80
81
82
83
# File 'lib/captive/cue.rb', line 77

def add_text(text)
  if self.text.nil?
    self.text = text
  else
    self.text += "\n" + text
  end
end

#as_json(**args) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/captive/cue.rb', line 89

def as_json(**args)
  if respond_to?(:instance_values) && instance_values.respond_to?(:as_json)
    instance_values.as_json(**args)
  else
    instance_variables.each_with_object({}) { |key, hash| hash[key[1..-1]] = instance_variable_get(key) }
  end
end

#durationObject



72
73
74
# File 'lib/captive/cue.rb', line 72

def duration
  end_time - start_time
end

#set_times(start_time:, end_time:) ⇒ Object



54
55
56
57
# File 'lib/captive/cue.rb', line 54

def set_times(start_time:, end_time:)
  self.start_time = start_time
  self.end_time = end_time
end