Class: Muzak::Playlist
- Inherits:
-
Object
- Object
- Muzak::Playlist
- Defined in:
- lib/muzak/playlist.rb
Overview
Represents a sequential list of songs for muzak.
Instance Attribute Summary collapse
-
#filename ⇒ String
The absolute path to the playlist on disk.
-
#songs ⇒ Array<Song>
The playlist's songs.
Class Method Summary collapse
-
.delete!(pname) ⇒ void
Deletes the given playlist from disk.
-
.exist?(pname) ⇒ Boolean
Whether or not the given playlist name already exists.
-
.load_playlists! ⇒ Hash{String => Playlist}
Instantiates all playlists by loading them from disk.
-
.path_for(pname) ⇒ String
The absolute path to the given playlist name.
-
.playlist_names ⇒ Array<String>
The names of all currently available playlists.
Instance Method Summary collapse
- #add(songs) ⇒ void
- #delete(songs) ⇒ void
-
#initialize(pname) ⇒ Playlist
constructor
Create a new Playlist with the given name, or load one by that name if it already exists.
-
#name ⇒ String
The playlist's name.
-
#shuffle! ⇒ void
Shuffles the internal order of the playlist's songs.
-
#sync! ⇒ void
Synchronizes the current instance with its disk representation.
-
#to_hash ⇒ Hash{String => Array<Song>}
Provides a hash representation of the current instance.
Constructor Details
#initialize(pname) ⇒ Playlist
Create a new Muzak::Playlist with the given name, or load one by that name if it already exists.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/muzak/playlist.rb', line 57 def initialize(pname) @filename = self.class.path_for pname if File.exist?(@filename) phash = YAML.load_file(@filename) @songs = phash["songs"] else @songs = [] end sync! end |
Instance Attribute Details
#filename ⇒ String
Returns the absolute path to the playlist on disk.
5 6 7 |
# File 'lib/muzak/playlist.rb', line 5 def filename @filename end |
#songs ⇒ Array<Song>
Returns the playlist's songs.
8 9 10 |
# File 'lib/muzak/playlist.rb', line 8 def songs @songs end |
Class Method Details
.delete!(pname) ⇒ void
If already instantiated, the playlist may still be present in memory (and may reappear on disk if modified in memory)
This method returns an undefined value.
Deletes the given playlist from disk.
27 28 29 |
# File 'lib/muzak/playlist.rb', line 27 def self.delete!(pname) File.delete(path_for(pname)) if exist? pname end |
.exist?(pname) ⇒ Boolean
Returns whether or not the given playlist name already exists.
18 19 20 |
# File 'lib/muzak/playlist.rb', line 18 def self.exist?(pname) File.exist?(path_for(pname)) end |
.load_playlists! ⇒ Hash{String => Playlist}
Instantiates all playlists by loading them from disk.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/muzak/playlist.rb', line 43 def self.load_playlists! playlists = {} playlists.default_proc = proc { |h, k| h[k] = Playlist.new(k) } playlist_names.each do |pname| playlists[pname] = Playlist.new(pname) end playlists end |
.path_for(pname) ⇒ String
Returns the absolute path to the given playlist name.
12 13 14 |
# File 'lib/muzak/playlist.rb', line 12 def self.path_for(pname) File.join(Config::PLAYLIST_DIR, pname) + ".yml" end |
.playlist_names ⇒ Array<String>
Returns the names of all currently available playlists.
32 33 34 35 36 37 38 |
# File 'lib/muzak/playlist.rb', line 32 def self.playlist_names Dir.entries(Config::PLAYLIST_DIR).reject do |ent| ent.start_with?(".") end.map do |ent| File.basename(ent, File.extname(ent)) end end |
Instance Method Details
#add(songs) ⇒ void
This method returns an undefined value.
77 78 79 80 81 82 83 84 85 |
# File 'lib/muzak/playlist.rb', line 77 def add(songs) # coerce a single song into an array [*songs].each do |song| next if @songs.include?(song) @songs << song end sync! end |
#delete(songs) ⇒ void
This method returns an undefined value.
90 91 92 93 94 |
# File 'lib/muzak/playlist.rb', line 90 def delete(songs) [*songs].each { |song| @songs.delete(song) } sync! end |
#name ⇒ String
Returns the playlist's name.
71 72 73 |
# File 'lib/muzak/playlist.rb', line 71 def name File.basename(@filename, File.extname(@filename)) end |
#shuffle! ⇒ void
This method returns an undefined value.
Shuffles the internal order of the playlist's songs.
98 99 100 |
# File 'lib/muzak/playlist.rb', line 98 def shuffle! @songs.shuffle! end |
#sync! ⇒ void
You shouldn't need to call this.
This method returns an undefined value.
Synchronizes the current instance with its disk representation.
105 106 107 |
# File 'lib/muzak/playlist.rb', line 105 def sync! File.open(@filename, "w") { |io| io.write to_hash.to_yaml } end |
#to_hash ⇒ Hash{String => Array<Song>}
Provides a hash representation of the current instance.
111 112 113 |
# File 'lib/muzak/playlist.rb', line 111 def to_hash { "songs" => @songs } end |