Class: Muzak::Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/muzak/playlist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pname) ⇒ Playlist

Create a new Muzak::Playlist with the given name, or load one by that name if it already exists.

Parameters:

  • pname (String)

    the playlist's name



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/muzak/playlist.rb', line 56

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

#filenameString

Returns the absolute path to the playlist on disk.

Returns:

  • (String)

    the absolute path to the playlist on disk



4
5
6
# File 'lib/muzak/playlist.rb', line 4

def filename
  @filename
end

#songsArray<Song>

Returns the playlist's songs.

Returns:

  • (Array<Song>)

    the playlist's songs



7
8
9
# File 'lib/muzak/playlist.rb', line 7

def songs
  @songs
end

Class Method Details

.delete!(pname) ⇒ void

Note:

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.

Parameters:

  • pname (String)

    the playlist's name



26
27
28
# File 'lib/muzak/playlist.rb', line 26

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.

Parameters:

  • pname (String)

    the playlist's name

Returns:

  • (Boolean)

    whether or not the given playlist name already exists



17
18
19
# File 'lib/muzak/playlist.rb', line 17

def self.exist?(pname)
  File.exist?(path_for(pname))
end

.load_playlists!Hash{String => Playlist}

Instantiates all playlists by loading them from disk.

Returns:



42
43
44
45
46
47
48
49
50
51
# File 'lib/muzak/playlist.rb', line 42

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.

Parameters:

  • pname (String)

    the playlist's name

Returns:

  • (String)

    the absolute path to the given playlist name



11
12
13
# File 'lib/muzak/playlist.rb', line 11

def self.path_for(pname)
  File.join(PLAYLIST_DIR, pname) + ".yml"
end

.playlist_namesArray<String>

Returns the names of all currently available playlists.

Returns:

  • (Array<String>)

    the names of all currently available playlists



31
32
33
34
35
36
37
# File 'lib/muzak/playlist.rb', line 31

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) ⇒ void

This method returns an undefined value.

Parameters:

  • songs (Song, Array<Song>)

    one or more songs to add to the playlist



76
77
78
79
80
81
82
83
84
# File 'lib/muzak/playlist.rb', line 76

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.

Parameters:

  • songs (Song, Array<Song>)

    one or more songs to delete from the playlist



89
90
91
92
93
# File 'lib/muzak/playlist.rb', line 89

def delete(songs)
  [*songs].each { |song| @songs.delete(song) }

  sync!
end

#nameString

Returns the playlist's name.

Returns:

  • (String)

    the playlist's name



70
71
72
# File 'lib/muzak/playlist.rb', line 70

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.



97
98
99
# File 'lib/muzak/playlist.rb', line 97

def shuffle!
  @songs.shuffle!
end

#sync!void

Note:

You shouldn't need to call this.

This method returns an undefined value.

Synchronizes the current instance with its disk representation.



104
105
106
# File 'lib/muzak/playlist.rb', line 104

def sync!
  File.open(@filename, "w") { |io| io.write to_hash.to_yaml }
end

#to_hashHash{String => Array<Song>}

Provides a hash representation of the current instance.

Returns:

  • (Hash{String => Array<Song>})

    the instance's state



110
111
112
# File 'lib/muzak/playlist.rb', line 110

def to_hash
  { "songs" => @songs }
end