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
34
35
36
37
38
39
# 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
          ref = line
          begin
            ref = GLib.filename_from_uri(ref)
          rescue
            # TODO: make cross-platform
            ref = File.expand_path(ref.gsub('\\', '/'), File.dirname(path))
          end
          
          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



41
42
43
# File 'lib/sh_playlist.rb', line 41

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

#save!Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sh_playlist.rb', line 49

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



45
46
47
# File 'lib/sh_playlist.rb', line 45

def songs
  return @songs.dup
end