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(name, songs) ⇒ Playlist

Returns a new instance of Playlist.



23
24
25
26
# File 'lib/muzak/playlist.rb', line 23

def initialize(name, songs)
  @name = name
  @songs = songs
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#songsObject

Returns the value of attribute songs.



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

def songs
  @songs
end

Class Method Details

.load_playlist(path) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/muzak/playlist.rb', line 13

def self.load_playlist(path)
  instance = allocate
  playlist_hash = YAML.load_file(path)

  instance.name = File.basename(path, File.extname(path))
  instance.songs = playlist_hash["songs"]

  instance
end

.playlist_namesObject



5
6
7
8
9
10
11
# File 'lib/muzak/playlist.rb', line 5

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(song) ⇒ Object



28
29
30
31
# File 'lib/muzak/playlist.rb', line 28

def add(song)
  return if @songs.include?(song)
  @songs << song
end

#delete(song) ⇒ Object



33
34
35
# File 'lib/muzak/playlist.rb', line 33

def delete(song)
  @songs.delete(song)
end

#shuffle!Object



37
38
39
# File 'lib/muzak/playlist.rb', line 37

def shuffle!
  @songs.shuffle!
end

#to_hashObject



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

def to_hash
  { "songs" => songs }
end