Module: Playlist::Format::SimpleText

Defined in:
lib/playlist/format/simple_text.rb

Overview

Module to parse and generate playlists with one line per track

Class Method Summary collapse

Class Method Details

.generate(playlist) ⇒ String

Generate a human readable list of tracks from a Playlist

Parameters:

Returns:

  • (String)

    the playlist with one line per track



38
39
40
# File 'lib/playlist/format/simple_text.rb', line 38

def generate(playlist)
  playlist.tracks.map { |t| "#{t.artist} - #{t.title}" }.join("\n") + "\n"
end

.parse(input) ⇒ Playlist

Parse a human readable list of tracks into a Playlist

Parameters:

  • input

    any object that responds to #each_line (either a String or a IO object)

Returns:



8
9
10
11
12
13
14
15
16
17
# File 'lib/playlist/format/simple_text.rb', line 8

def parse(input)
  Playlist.new do |playlist|
    input.each_line do |line|
      next if line =~ /^#/

      track = parse_line(line.strip)
      playlist.tracks << track unless track.nil?
    end
  end
end

.parse_line(line) ⇒ Track

Parse a single line from a playlist into a Track obect

Parameters:

  • line (String)

    any object that responds to #each_line

Returns:

  • (Track)

    a new Track object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/playlist/format/simple_text.rb', line 22

def parse_line(line)
  matches = line.match(
    /^(\d{1,2}[:.]\d{1,2}([:.]\d{1,2})?)?\s*(.+?) - (.+?)$/
  )
  unless matches.nil?
    Playlist::Track.new(
      :start_time => matches[1],
      :creator => matches[3].strip,
      :title => matches[4].strip
    )
  end
end