Class: Sh::Playlist

Inherits:
Object show all
Defined in:
lib/sh_playlist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Playlist

Returns a new instance of Playlist.



5
6
7
8
# File 'lib/sh_playlist.rb', line 5

def initialize name
  @name = name
  @songs = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/sh_playlist.rb', line 3

def name
  @name
end

Class Method Details

.parse(path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sh_playlist.rb', line 10

def self.parse(path)
  list = Playlist.new File.basename(path)
  open(path) do |f|
    while line = f.gets
      line.strip!
      unless line[0] == ?# or line.empty?
        begin
          # TODO: make cross-platform
          ref = File.expand_path(line.gsub('\\', '/'), File.dirname(path))
          if File.exists? ref
            if Global::SUPPORTED_EXTENSIONS.include? File.extname(ref)
              list << (Song.first(:path => ref) || Database.add_song(ref))
            end
          else
            Log.info "File at #{ref} not found"
          end
        rescue Exception
          Log.info "Couldn't parse line in playlist: #{line}", $!
        end
      end
    end
  end
  return list
end

Instance Method Details

#<<(song) ⇒ Object



35
36
37
# File 'lib/sh_playlist.rb', line 35

def <<(song)
  @songs << song
end

#save!Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sh_playlist.rb', line 43

def save!
  path = Global::PATHS[:playlists_file]
  playlists = []
  if File.exists? path
    playlists = YAML.load_file path
  end
  playlists.reject! {|name, paths| name == @name}
  song_paths = @songs.map {|song| song.path}
  playlists << [@name, song_paths]
  open(path, 'w') {|f| f.puts playlists.to_yaml}
end

#songsObject



39
40
41
# File 'lib/sh_playlist.rb', line 39

def songs
  return @songs.dup
end