Class: Muzak::Playlist
- Inherits:
-
Object
- Object
- Muzak::Playlist
- Defined in:
- lib/muzak/playlist.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#songs ⇒ Object
Returns the value of attribute songs.
Class Method Summary collapse
- .delete!(pname) ⇒ Object
- .exist?(pname) ⇒ Boolean
- .path_for(pname) ⇒ Object
- .playlist_names ⇒ Object
Instance Method Summary collapse
- #add(songs) ⇒ Object
- #delete(songs) ⇒ Object
-
#initialize(pname) ⇒ Playlist
constructor
A new instance of Playlist.
- #name ⇒ Object
- #shuffle! ⇒ Object
- #sync! ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(pname) ⇒ Playlist
Returns a new instance of Playlist.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/muzak/playlist.rb', line 25 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 ⇒ Object
Returns the value of attribute filename.
3 4 5 |
# File 'lib/muzak/playlist.rb', line 3 def filename @filename end |
#songs ⇒ Object
Returns the value of attribute songs.
3 4 5 |
# File 'lib/muzak/playlist.rb', line 3 def songs @songs end |
Class Method Details
.delete!(pname) ⇒ Object
13 14 15 |
# File 'lib/muzak/playlist.rb', line 13 def self.delete!(pname) File.delete(path_for(pname)) if exist? pname end |
.exist?(pname) ⇒ Boolean
9 10 11 |
# File 'lib/muzak/playlist.rb', line 9 def self.exist?(pname) File.exist?(path_for(pname)) end |
.path_for(pname) ⇒ Object
5 6 7 |
# File 'lib/muzak/playlist.rb', line 5 def self.path_for(pname) File.join(PLAYLIST_DIR, pname) + ".yml" end |
.playlist_names ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/muzak/playlist.rb', line 17 def self.playlist_names Dir.entries(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) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/muzak/playlist.rb', line 42 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) ⇒ Object
52 53 54 55 56 |
# File 'lib/muzak/playlist.rb', line 52 def delete(songs) [*songs].each { |song| @songs.delete(song) } sync! end |
#name ⇒ Object
38 39 40 |
# File 'lib/muzak/playlist.rb', line 38 def name File.basename(@filename, File.extname(@filename)) end |
#shuffle! ⇒ Object
58 59 60 |
# File 'lib/muzak/playlist.rb', line 58 def shuffle! @songs.shuffle! end |
#sync! ⇒ Object
62 63 64 |
# File 'lib/muzak/playlist.rb', line 62 def sync! File.open(@filename, "w") { |io| io.write to_hash.to_yaml } end |
#to_hash ⇒ Object
66 67 68 |
# File 'lib/muzak/playlist.rb', line 66 def to_hash { "songs" => @songs } end |