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
# File 'lib/sh_playlist.rb', line 10

def self.parse(path)
  list = Playlist.new path.match(/\/([^\/]*)$/).captures.last
  open(path) do |f|
    while line = f.gets
      line.strip!
      unless line[0] == ?# or line.empty?
      	# TODO: make cross-platform
        ref = File.expand_path(line.gsub('\\', '/'), File.dirname(path))
        if File.exists? ref
          if Sh::Global::SUPPORTED_EXTENSIONS.include? File.extname(ref)
            list << ($db.songs(:path => ref).first || Sh::Song.new(ref).read_tags!)
          end
        else
          Sh::Log.info "File at #{ref} not found"
        end
      end
    end
  end
  return list
end

Instance Method Details

#<<(song) ⇒ Object



31
32
33
# File 'lib/sh_playlist.rb', line 31

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

#save!Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sh_playlist.rb', line 39

def save!
	path = Sh::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



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

def songs
  return @songs.dup
end