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
-
.generate(playlist) ⇒ String
Generate a human readable list of tracks from a Playlist.
-
.parse(input) ⇒ Playlist
Parse a human readable list of tracks into a Playlist.
-
.parse_line(line) ⇒ Track
Parse a single line from a playlist into a Track obect.
Class Method Details
.generate(playlist) ⇒ String
Generate a human readable list of tracks from a Playlist
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
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
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 |