class AudioPlayback::Position

A time position in a sound

Constants

FORMAT

Time format like (hh:)(mm:)ss(.ss)

UNITS

Attributes

seconds[R]
to_seconds[R]

Public Class Methods

new(seconds_or_time) click to toggle source

@param [Numeric, String] seconds_or_time Time as (hh:)(mm:)ss(.ss)

# File lib/audio-playback/position.rb, line 25
def initialize(seconds_or_time)
  seconds_or_time = seconds_or_time.to_s
  validate_time(seconds_or_time)
  populate(seconds_or_time)
end

Public Instance Methods

*(another) click to toggle source

Multiply the seconds value of this Position by the given value @param [Numeric, Position] another @return [Float]

# File lib/audio-playback/position.rb, line 34
def *(another)
  @seconds * another.to_f
end
+(another) click to toggle source

Add the seconds value of this Position to the given value @param [Numeric, Position] another @return [Float]

# File lib/audio-playback/position.rb, line 41
def +(another)
  @seconds + another.to_f
end

Private Instance Methods

populate(seconds_or_time) click to toggle source

Populate the seconds ivar using the time that was passed into the constructor @param [Numeric, String] seconds_or_time Time as (hh:)(mm:)ss(.ss) @return [Float]

# File lib/audio-playback/position.rb, line 77
def populate(seconds_or_time)
  segments = seconds_or_time.split(":").map(&:to_f).reverse
  validate_segments(segments)
  @seconds = 0
  segments.each_with_index do |segment, i|
    @seconds += segment * UNITS[i]
  end
  @seconds
end
validate_segments(segments) click to toggle source

Validate that the segments of the time that was passed into the constructor are valid. For example that the minutes and or seconds values are below 60. Raises InvalidTime error if not @param [Array<String>] segments Time as [(hh), (mm), ss(.ss)] @return [Boolean]

# File lib/audio-playback/position.rb, line 63
def validate_segments(segments)
  seconds = segments[0]
  minutes = segments[1]
  [seconds, minutes].compact.each do |segment|
    if segment >= 60
      raise(InvalidTime)
    end
  end
  true
end
validate_time(seconds_or_time) click to toggle source

Validate that the time that was passed into the constructor is in the correct format. Raises InvalidTime error if not @param [Numeric, String] seconds_or_time Time as (hh:)(mm:)ss(.ss) @return [Boolean]

# File lib/audio-playback/position.rb, line 51
def validate_time(seconds_or_time)
  unless seconds_or_time.match(FORMAT)
    raise(InvalidTime)
  end
  true
end