Class: CultomePlayer::Player::Playlist::Playlists

Inherits:
Object
  • Object
show all
Includes:
Utils, Enumerable
Defined in:
lib/cultome_player/player/playlist.rb

Instance Method Summary collapse

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.

Parameters:

  • data (#each) (defaults to: nil)

    A collection of items to add to playlist



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.

Parameters:

  • value (List<Object>)

    The appended of the playlist.



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.

Parameters:

  • value (List<Object>)

    The new contents of the playlist.



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.

Returns:

  • (Playlists)

    Playlists with selected playlists inside.



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_listString

Returns a string representation of the playlists.

Returns:

  • (String)

    A 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.

Parameters:

  • idx (Integer)

    The positional index of the element required.

Returns:

  • (List<Object>, Object)

    The positional elements in the playlists.



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

#currentList<Object>, Object

Returns the current element in playlists.

Returns:

  • (List<Object>, Object)

    The current element(s) in playlist(s).



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

#eachIterator

Creates an interator for the playlists

Returns:

  • (Iterator)

    Iterator over 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_songIterator

Creates an interator for all the songs in all the playlists

Returns:

  • (Iterator)

    Iterator over all the songs.



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.

Returns:

  • (Boolean)

    True if there is any playlist registered. False otherwise.



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

#nextList<Object>, Object

Returns the next song in playlist, which means the new current song.

Returns:

  • (List<Object>, Object)

    The next element(s) in playlist(s).



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.

Returns:

  • (List<Boolean>, Boolean)

    True if the the playlist has more elements, False otherwise.



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

#orderObject

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_indexList<Integer>, Integer

Return the play index in the playlists.

Returns:

  • (List<Integer>, Integer)

    Indexes of the playlists.



155
156
157
# File 'lib/cultome_player/player/playlist.rb', line 155

def play_index
  return first_or_map :idx
end

#popList<Object>, Object

Removes the last element in the playlists.

Returns:

  • (List<Object>, Object)

    The las elements 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.

Parameters:

  • name (Symbol)

    The name of the new playlist.

  • value (List<Object>) (defaults to: nil)

    Optional data to initialize the 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.

Parameters:

  • name (Symbol)

    The name of the new playlist.

Returns:

  • (Boolean)

    True if previously registered, False otherwise.



42
43
44
# File 'lib/cultome_player/player/playlist.rb', line 42

def registered?(name)
  @data.has_key?(name)
end

#remove_nextList<Object>, Object

Remove the next element in playlist.

Returns:

  • (List<Object>, Object)

    The next element(s) in playlist(s).



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.

Returns:

  • (List<Boolean>, Boolean)

    Indexes of 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.

Returns:

  • (List<Object>, Object)

    The previous element(s) in playlist(s).



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

#shuffleObject

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.

Returns:

  • (List<Boolean>, Boolean)

    True if playlist is shuffling. False otherwise.



234
235
236
# File 'lib/cultome_player/player/playlist.rb', line 234

def shuffling?
  return first_or_map :shuffled
end

#sizeInteger

The number of registered playlists.

Returns:

  • (Integer)

    The size of registered playlist.



190
191
192
# File 'lib/cultome_player/player/playlist.rb', line 190

def size
  @data.size
end

#to_aList<Object> Also known as: songs

Return a list with all the songs in all the playlists.

Returns:

  • (List<Object>)

    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