class CultomePlayer::Player::Playlist::Playlists

Public Class Methods

new(data=nil) click to toggle source

Initialize a playlist with optional information to fill.

@param data [#each] A collection of items to add to playlist

# File lib/cultome_player/player/playlist.rb, line 24
def initialize(data=nil)
  @data = {}
  data.each{|arr| register(*arr) } unless data.nil?
end

Public Instance Methods

<<(value) click to toggle source

Append the content of the playlist with the content of parameter.

@param value [List<Object>] The appended of the playlist.

# 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) click to toggle source

Replace the content of the playlist with the content of parameter.

@param value [List<Object>] The new contents of the playlist.

# File lib/cultome_player/player/playlist.rb, line 81
def <=(value)
  @data.keys.each{|name| replace(name, value) }
end
[](*names) click to toggle source

Creates a new playlist object that contains the playlists named in parameters.

@return [Playlists] Playlists with selected playlists inside.

# 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() click to toggle source

Returns a string representation of the playlists.

@return [String] A representation of the playlists.

# 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) click to toggle source

Returns the elements in the playlist.

@param idx [Integer] The positional index of the element required. @return [List<Object>, Object] The positional elements in the playlists.

# 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() click to toggle source

Returns the current element in playlists.

@return [List<Object>, Object] The current element(s) in playlist(s).

# 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() { |info| ... } click to toggle source

Creates an interator for the playlists

@return [Iterator] Iterator over the playlists.

# File lib/cultome_player/player/playlist.rb, line 57
def each
  @data.values.each{|info| yield info[:list] }
end
each_song() { |song, idx += 1| ... } click to toggle source

Creates an interator for all the songs in all the playlists

@return [Iterator] Iterator over all the songs.

# 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?() click to toggle source

Check if there is playlists registered.

@return [Boolean] True if there is any playlist registered. False otherwise.

# 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() click to toggle source

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

@return [List<Object>,Object] The next element(s) in playlist(s).

# 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?() click to toggle source

Check if there is another element in playlists.

@return [List<Boolean>, Boolean] True if the the playlist has more elements, False otherwise.

# 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() click to toggle source

Order the playlists and reset the indexes.

# 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() click to toggle source

Return the play index in the playlists.

@return [List<Integer>, Integer] Indexes of the playlists.

# File lib/cultome_player/player/playlist.rb, line 155
def play_index
  return first_or_map :idx
end
pop() click to toggle source

Removes the last element in the playlists.

@return [List<Object>, Object] The las elements in the playlists.

# 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) click to toggle source

Register a playlist.

@param name [Symbol] The name of the new playlist. @param value [List<Object>] Optional data to initialize the playlist.

# 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) click to toggle source

Check if a playlist is registered.

@param name [Symbol] The name of the new playlist. @return [Boolean] True if previously registered, False otherwise.

# File lib/cultome_player/player/playlist.rb, line 42
def registered?(name)
  @data.has_key?(name)
end
remove_next() click to toggle source

Remove the next element in playlist.

@return [List<Object>,Object] The next element(s) in playlist(s).

# 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) click to toggle source

Change the repeat status in the playlists.

# File lib/cultome_player/player/playlist.rb, line 167
def repeat(value)
  @data.values.each{|info| info[:repeat] = is_true_value?(value) }
end
repeat?() click to toggle source

Return the repeat status in the playlists.

@return [List<Boolean>, Boolean] Indexes of the playlists.

# File lib/cultome_player/player/playlist.rb, line 162
def repeat?
  return first_or_map :repeat
end
rewind_by(idx) click to toggle source

Returns the previous song in playlist.

@return [List<Object>,Object] The previous element(s) in playlist(s).

# 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() click to toggle source

Shuffle the playlists and reset the indexes.

# 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?() click to toggle source

Check the status of shuffling in playlists.

@return [List<Boolean>, Boolean] True if playlist is shuffling. False otherwise.

# File lib/cultome_player/player/playlist.rb, line 234
def shuffling?
  return first_or_map :shuffled
end
size() click to toggle source

The number of registered playlists.

@return [Integer] The size of registered playlist.

# File lib/cultome_player/player/playlist.rb, line 190
def size
  @data.size
end
songs()
Alias for: to_a
to_a() click to toggle source

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

@return [List<Object>] A list with all the songs in all the playlists.

# File lib/cultome_player/player/playlist.rb, line 197
def to_a
  @data.values.reduce([]){|acc,info| acc + info[:list]}
end
Also aliased as: songs

Private Instance Methods

each_next(&block) click to toggle source

Returns an array with songs, if multiple playlists and only one if single playlist is available

# File lib/cultome_player/player/playlist.rb, line 256
def each_next(&block)
  nexts = each_next_with_index(&block).compact
  raise "playlist empty:no songs in playlists" if nexts.empty?
  raise "playlist empty:no songs in one of the playlists" if nexts.size != @data.size
  return nexts.first if nexts.size == 1
  return nexts
end
each_next_with_index() { |info, nxt_idx| ... } click to toggle source

Returns the non-empty playlists yielded by the block

# File lib/cultome_player/player/playlist.rb, line 246
def each_next_with_index
  @data.values
  .select{|info| !info[:list].empty? }
  .map do |info|
    nxt_idx = next_idx(info[:idx], info[:list].size, info[:repeat])
    nxt_idx.nil? ? nil : yield(info, nxt_idx)
  end
end
first_or_map(attr) click to toggle source
# File lib/cultome_player/player/playlist.rb, line 240
def first_or_map(attr)
  return @data.values.first[attr] if @data.size == 1
  return @data.values.map{|info| info[attr] }
end
next_idx(actual_idx, size, repeat) click to toggle source
# File lib/cultome_player/player/playlist.rb, line 264
def next_idx(actual_idx, size, repeat)
  next_idx = actual_idx + 1
  next_idx = next_idx % size if repeat
  return nil if next_idx >= size
  return next_idx
end
replace(name, value) click to toggle source
# File lib/cultome_player/player/playlist.rb, line 271
def replace(name, value)
  @data[name][:list].replace value
  @data[name][:idx] = -1
end
validate(names) click to toggle source
# File lib/cultome_player/player/playlist.rb, line 276
def validate(names)
  raise 'unknown playlist:playlist is not registered' if names.any?{|n| !@data.keys.include?(n) }
end