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
35 36 37 |
# File 'lib/playlist/format/simple_text.rb', line 35 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 |
# File 'lib/playlist/format/simple_text.rb', line 22 def parse_line(line) if line =~ /^(\d{1,2}[:.]\d{1,2}([:.]\d{1,2})?)?\s*(.+?) - (.+?)$/ Playlist::Track.new( :start_time => Regexp.last_match(1), :creator => Regexp.last_match(3).strip, :title => Regexp.last_match(4).strip ) end end |