Class: CultomePlayer::Player::Playlist::Playlists
- Inherits:
-
Object
- Object
- CultomePlayer::Player::Playlist::Playlists
- Includes:
- Utils, Enumerable
- Defined in:
- lib/cultome_player/player/playlist.rb
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Append the content of the playlist with the content of parameter.
-
#<=(value) ⇒ Object
Replace the content of the playlist with the content of parameter.
-
#[](*names) ⇒ Playlists
Creates a new playlist object that contains the playlists named in parameters.
-
#as_list ⇒ String
Returns a string representation of the playlists.
-
#at(idx) ⇒ List<Object>, Object
Returns the elements in the playlist.
-
#current ⇒ List<Object>, Object
Returns the current element in playlists.
-
#each ⇒ Iterator
Creates an interator for the playlists.
-
#each_song ⇒ Iterator
Creates an interator for all the songs in all the playlists.
-
#empty? ⇒ Boolean
Check if there is playlists registered.
-
#initialize(data = nil) ⇒ Playlists
constructor
Initialize a playlist with optional information to fill.
-
#next ⇒ List<Object>, Object
Returns the next song in playlist, which means the new current song.
-
#next? ⇒ List<Boolean>, Boolean
Check if there is another element in playlists.
-
#order ⇒ Object
Order the playlists and reset the indexes.
-
#play_index ⇒ List<Integer>, Integer
Return the play index in the playlists.
-
#pop ⇒ List<Object>, Object
Removes the last element in the playlists.
-
#register(name, value = nil) ⇒ Object
Register a playlist.
-
#registered?(name) ⇒ Boolean
Check if a playlist is registered.
-
#remove_next ⇒ List<Object>, Object
Remove the next element in playlist.
-
#repeat(value) ⇒ Object
Change the repeat status in the playlists.
-
#repeat? ⇒ List<Boolean>, Boolean
Return the repeat status in the playlists.
-
#rewind_by(idx) ⇒ List<Object>, Object
Returns the previous song in playlist.
-
#shuffle ⇒ Object
Shuffle the playlists and reset the indexes.
-
#shuffling? ⇒ List<Boolean>, Boolean
Check the status of shuffling in playlists.
-
#size ⇒ Integer
The number of registered playlists.
-
#to_a ⇒ List<Object>
(also: #songs)
Return a list with all the songs in all the playlists.
Methods included from Utils
#arrange_in_columns, #display, #display_over, #ensure_db_schema, #is_true_value?, #recreate_db_schema, #swallow_stdout, #to_display_list, #with_connection
Constructor Details
#initialize(data = nil) ⇒ Playlists
Initialize a playlist with optional information to fill.
24 25 26 27 |
# File 'lib/cultome_player/player/playlist.rb', line 24 def initialize(data=nil) @data = {} data.each{|arr| register(*arr) } unless data.nil? end |
Instance Method Details
#<<(value) ⇒ Object
Append the content of the playlist with the content of parameter.
88 89 90 91 92 93 94 |
# File 'lib/cultome_player/player/playlist.rb', line 88 def <<(value) if value.respond_to?(:each) @data.values.each{|info| value.each{|v| info[:list] << v } } else @data.values.each{|info| info[:list] << value } end end |
#<=(value) ⇒ Object
Replace the content of the playlist with the content of parameter.
81 82 83 |
# File 'lib/cultome_player/player/playlist.rb', line 81 def <=(value) @data.keys.each{|name| replace(name, value) } end |
#[](*names) ⇒ Playlists
Creates a new playlist object that contains the playlists named in parameters.
72 73 74 75 76 |
# File 'lib/cultome_player/player/playlist.rb', line 72 def [](*names) validate names selected = @data.select{|name,info| names.include?(name) } return Playlists.new(selected) end |
#as_list ⇒ String
Returns a string representation of the playlists.
215 216 217 218 219 |
# File 'lib/cultome_player/player/playlist.rb', line 215 def as_list list = "" each_song{|s,i| list << "#{i}. #{s.to_s}\n" } return list end |
#at(idx) ⇒ List<Object>, Object
Returns the elements in the playlist.
207 208 209 210 |
# File 'lib/cultome_player/player/playlist.rb', line 207 def at(idx) return @data.values.first[:list].at(idx) if @data.size == 1 return @data.values.collect{|info| info[:list].at(idx) } end |
#current ⇒ List<Object>, Object
Returns the current element in playlists.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/cultome_player/player/playlist.rb', line 174 def current currents = @data.values .select{|info| info[:idx] >= 0} .map do |info| info[:list].at info[:idx] end return nil if currents.empty? raise 'no current:no current song in one of the playlists' if @data.size != currents.size return currents.first if currents.size == 1 return currents end |
#each ⇒ Iterator
Creates an interator for the playlists
57 58 59 |
# File 'lib/cultome_player/player/playlist.rb', line 57 def each @data.values.each{|info| yield info[:list] } end |
#each_song ⇒ Iterator
Creates an interator for all the songs in all the playlists
49 50 51 52 |
# File 'lib/cultome_player/player/playlist.rb', line 49 def each_song idx = 0 @data.values.each{|info| info[:list].each{|song| yield song, idx += 1 } } end |
#empty? ⇒ Boolean
Check if there is playlists registered.
64 65 66 67 |
# File 'lib/cultome_player/player/playlist.rb', line 64 def empty? return @data.values.first[:list].empty? if @data.size == 1 @data.empty? end |
#next ⇒ List<Object>, Object
Returns the next song in playlist, which means the new current song.
126 127 128 129 130 131 |
# File 'lib/cultome_player/player/playlist.rb', line 126 def next each_next do |info, nxt_idx| info[:idx] = nxt_idx info[:list].at nxt_idx end end |
#next? ⇒ List<Boolean>, Boolean
Check if there is another element in playlists.
224 225 226 227 228 229 |
# File 'lib/cultome_player/player/playlist.rb', line 224 def next? nexts = each_next_with_index{|info, nxt_idx| nxt_idx } has_nexts = nexts.map{|nxt_idx| !nxt_idx.nil? } return has_nexts.first if has_nexts.size == 1 return has_nexts end |
#order ⇒ Object
Order the playlists and reset the indexes.
115 116 117 118 119 120 121 |
# File 'lib/cultome_player/player/playlist.rb', line 115 def order @data.values.each do |info| info[:list].sort! info[:idx] = -1 info[:shuffled] = false end end |
#play_index ⇒ List<Integer>, Integer
Return the play index in the playlists.
155 156 157 |
# File 'lib/cultome_player/player/playlist.rb', line 155 def play_index return first_or_map :idx end |
#pop ⇒ List<Object>, Object
Removes the last element in the playlists.
99 100 101 102 103 |
# File 'lib/cultome_player/player/playlist.rb', line 99 def pop last_ones = collect{|list| list.pop } return last_ones.first if last_ones.size == 1 return last_ones end |
#register(name, value = nil) ⇒ Object
Register a playlist.
33 34 35 36 |
# File 'lib/cultome_player/player/playlist.rb', line 33 def register(name, value=nil) raise 'invalid registry:playlist already registered' unless @data[name].nil? @data[name] = value.nil? ? {list: [], idx: -1, repeat: true, shuffled: false} : value end |
#registered?(name) ⇒ Boolean
Check if a playlist is registered.
42 43 44 |
# File 'lib/cultome_player/player/playlist.rb', line 42 def registered?(name) @data.has_key?(name) end |
#remove_next ⇒ List<Object>, Object
Remove the next element in playlist.
146 147 148 149 150 |
# File 'lib/cultome_player/player/playlist.rb', line 146 def remove_next each_next do |info, nxt_idx| info[:list].delete_at nxt_idx end end |
#repeat(value) ⇒ Object
Change the repeat status in the playlists.
167 168 169 |
# File 'lib/cultome_player/player/playlist.rb', line 167 def repeat(value) @data.values.each{|info| info[:repeat] = is_true_value?(value) } end |
#repeat? ⇒ List<Boolean>, Boolean
Return the repeat status in the playlists.
162 163 164 |
# File 'lib/cultome_player/player/playlist.rb', line 162 def repeat? return first_or_map :repeat end |
#rewind_by(idx) ⇒ List<Object>, Object
Returns the previous song in playlist.
136 137 138 139 140 141 |
# File 'lib/cultome_player/player/playlist.rb', line 136 def rewind_by(idx) each_next do |info, nxt_idx| info[:idx] -= idx info[:list].at info[:idx] end end |
#shuffle ⇒ Object
Shuffle the playlists and reset the indexes.
106 107 108 109 110 111 112 |
# File 'lib/cultome_player/player/playlist.rb', line 106 def shuffle @data.values.each do |info| info[:list].shuffle! info[:shuffled] = true info[:idx] = -1 end end |
#shuffling? ⇒ List<Boolean>, Boolean
Check the status of shuffling in playlists.
234 235 236 |
# File 'lib/cultome_player/player/playlist.rb', line 234 def shuffling? return first_or_map :shuffled end |
#size ⇒ Integer
The number of registered playlists.
190 191 192 |
# File 'lib/cultome_player/player/playlist.rb', line 190 def size @data.size end |
#to_a ⇒ List<Object> Also known as: songs
Return a list with all the songs in all the playlists.
197 198 199 |
# File 'lib/cultome_player/player/playlist.rb', line 197 def to_a @data.values.reduce([]){|acc,info| acc + info[:list]} end |