Class: MikePlayer::Playlist

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Playlist

Returns a new instance of Playlist.



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

def initialize(filename)
  @filename = filename
  @songs    = []
  @length   = 0

  load_songs

  @loaded_song_count = self.size
end

Instance Attribute Details

#songsObject (readonly)

Returns the value of attribute songs.



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

def songs
  @songs
end

Instance Method Details

#<<(song) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/mikeplayer/playlist.rb', line 15

def <<(song)
  if ((true == File.file?(song)) && (false == @songs.any? { |s| s.filename == song }))
    @songs << Song.new(song)
    @length += @songs.last.length
  end

  return self
end

#add_random(n, directory) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/mikeplayer/playlist.rb', line 24

def add_random(n, directory)
  files = Dir.glob(File.join(directory, "**", "*.mp3"), File::FNM_CASEFOLD) - @songs

  files.sample(n).each do |file|
    self << file
  end

  return self
end

#find_song(file, directory) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mikeplayer/playlist.rb', line 34

def find_song(file, directory)
  if (true == File.file?(file))
    self << file
  else
    Dir.glob(File.join(directory, "**", "*#{file}*"), File::FNM_CASEFOLD).each do |f|
      self << f
    end
  end

  return self
end

#get(i) ⇒ Object



46
47
48
# File 'lib/mikeplayer/playlist.rb', line 46

def get(i)
  return @songs[i]
end

#infoObject



66
67
68
# File 'lib/mikeplayer/playlist.rb', line 66

def info
  return "#{self.name} loaded #{@loaded_song_count} and added #{self.size - @loaded_song_count} songs with length #{Song.as_duration_str(@length)}"
end

#nameObject



62
63
64
# File 'lib/mikeplayer/playlist.rb', line 62

def name
  return File.basename(@filename, Settings::PL_FILE_ENDING)
end

#saveObject



56
57
58
59
60
# File 'lib/mikeplayer/playlist.rb', line 56

def save
  File.open(@filename, 'w') do |f|
    f.puts(JSON.pretty_generate(@songs.map {|s| s.to_json }))
  end
end

#shuffle!Object



50
51
52
53
54
# File 'lib/mikeplayer/playlist.rb', line 50

def shuffle!
  @songs.shuffle!

  return self
end

#sizeObject



76
77
78
# File 'lib/mikeplayer/playlist.rb', line 76

def size
  return @songs.size
end

#song_info(i) ⇒ Object



70
71
72
73
74
# File 'lib/mikeplayer/playlist.rb', line 70

def song_info(i)
  song_i_str = "#{i + 1}".rjust(self.size.to_s.size)

  return "Playing (#{song_i_str}/#{self.size}): #{get(i).info}".freeze
end