Class: Subtitle
- Inherits:
-
Object
- Object
- Subtitle
- Defined in:
- lib/subtitle.rb
Instance Attribute Summary collapse
-
#chunks ⇒ Object
readonly
Returns the value of attribute chunks.
Instance Method Summary collapse
-
#initialize(input) ⇒ Subtitle
constructor
A new instance of Subtitle.
- #initialize_copy(source) ⇒ Object
- #shift(seconds) ⇒ Object
- #shift!(seconds) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(input) ⇒ Subtitle
Returns a new instance of Subtitle.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/subtitle.rb', line 12 def initialize input if input.is_a? Array @chunks = input else input = input.read if input.respond_to? :read @chunks = [] chunks = input.split /(\r?\n){2}/ chunks.each do |chunk| chunk.strip! @chunks << SubtitleChunk.new(chunk) unless chunk.empty? end end end |
Instance Attribute Details
#chunks ⇒ Object (readonly)
Returns the value of attribute chunks.
10 11 12 |
# File 'lib/subtitle.rb', line 10 def chunks @chunks end |
Instance Method Details
#initialize_copy(source) ⇒ Object
28 29 30 31 32 |
# File 'lib/subtitle.rb', line 28 def initialize_copy source super @chunks = @chunks.dup @chunks.map! { |chunk| chunk.dup } end |
#shift(seconds) ⇒ Object
38 39 40 41 |
# File 'lib/subtitle.rb', line 38 def shift seconds subtitle = self.dup subtitle.shift! seconds end |
#shift!(seconds) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/subtitle.rb', line 43 def shift! seconds @chunks.each do |chunk| chunk.shift! seconds end self end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/subtitle.rb', line 34 def to_s @chunks.map.with_index { |chunk, order| chunk.to_s(order + 1) }.join "\n" end |