module CultomePlayer::Player::Interface::Helper

Constants

VALID_SONG_ATTR

Public Class Methods

new(lbl, tot) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 199
def initialize(lbl, tot)
  @_label, @_actual = lbl.gsub(/\{total\}/, tot.to_s), 0
end

Public Instance Methods

connect_response_msg(imported, updated) click to toggle source

Select the connect action message depending on the imported and updated parameters.

@param imported [Integer] The number of imported songs. @param updated [Integer] The number of updated songs. @return [String] The appropiated message to show to user.

# File lib/cultome_player/player/interface/helper.rb, line 182
def connect_response_msg(imported, updated)
  message = ""
  if imported > 0
    message += "Songs imported: #{imported}."
  end

  if updated > 0
    message += "Songs updated: #{updated}."
  end

  return message
end
format_secs(secs) click to toggle source

Returns a string representation of a number of seconds in the format: mm:ss

@param secs [Integer] Number of seconds to be represented. @return [String] The number of seconds formatted as mm:ss.

# File lib/cultome_player/player/interface/helper.rb, line 11
def format_secs(secs)
  mins = secs.to_i / 60
  secs_left = secs.to_i % 60
  return "#{mins.to_s.rjust(2, "0")}:#{secs_left.to_s.rjust(2, "0")}"
end
get_files_in_tree(path, *extensions) click to toggle source

Return a list of absolute path of files in the path which has extension.

@param path [String] The path to searlook for files. @param extension [List<String>] The list of extension to filter the files with. @return [List<String>] The absolute paths to the files found.

# File lib/cultome_player/player/interface/helper.rb, line 60
def get_files_in_tree(path, *extensions)
  return extensions.each_with_object([]) do |ext, files|
    files << Dir.glob("#{path}/**/*.#{ext}")
  end.flatten
end
get_from_playlists(lists) click to toggle source

Get a list of songs from selected playlists, only if the playlist exist.

@param lists [List<Symbol>] The names of the playlists to check. @return [List<Song>] The songs in the valid playlists.

# File lib/cultome_player/player/interface/helper.rb, line 144
def get_from_playlists(lists)
  valid_lists = lists.select{|list_name| playlist?(list_name) }
  songs = playlists[*valid_lists].songs
  return songs.uniq{|s| s.id }
end
get_progress_bar(current, total=100, size=10) click to toggle source

(see get_progress_bar_with_labels)

# File lib/cultome_player/player/interface/helper.rb, line 32
def get_progress_bar(current, total=100, size=10)
  factor = total > 0 ? current / total.to_f : 0
  bars = ( factor * size ).floor
  total = "-" * size
  total[0,bars] = "#" * bars
  return "|#{total}>"
end
get_progress_bar_with_labels(current, total=100, size=10, left='', right='') click to toggle source

Returns a representation of a progress bar.

@param current [Integer] The actual progress. @param total [Integer] The total progress to achive. @param total [Integer] Optional, the total progress to achive. Default 100. @param size [Integer] Optional, the width of the bar. Default 10. @param left [String] Optional, prefix to append. Default ''. @param right [String] Optional, postfix to append. Default ''. @return [String] The string representation of a progress bar.

# File lib/cultome_player/player/interface/helper.rb, line 26
def get_progress_bar_with_labels(current, total=100, size=10, left='', right='')
  bar = get_progress_bar(current, total, size)
  return "#{left} #{bar} #{right}".strip
end
insert_song(new_info) click to toggle source

Insert a new song into database. Except if its already present by path.

@param new_info [List<Hash>] Has contains the keys :artist_id, :album_id, :drive_id, :relative_path, :library_path (optional). @return [Integer] The number of songs writed.

# File lib/cultome_player/player/interface/helper.rb, line 70
def insert_song(new_info)
  existing_paths = get_unique_paths
  to_be_processed = new_info.select{|s| !existing_paths.include?(s[:file_path]) }
  progress = progress_label("Adding songs {actual}/{total}", to_be_processed.size)
  return to_be_processed.count do |info|
    display_over c15(progress.increment)
    write_song(info)
  end
end
play_inline?(cmd) click to toggle source

Check if a command has the format to be considered a ply inline (dont create a playlist).

@param cmd [Command] The command to check. @return [Boolean] True if is considered to be played inline. False otherwise.

# File lib/cultome_player/player/interface/helper.rb, line 165
def play_inline?(cmd)
  if cmd.action == "play"
    return true if cmd.params.all?{|p| p.type == :number }
    if cmd.params.size == 1
      p = cmd.params.first
      return p.type == :object && p.value == :song
    end
  end

  return false
end
play_queue() click to toggle source

Play the next song in queue playlist.

@return [Song] The song programed.

# File lib/cultome_player/player/interface/helper.rb, line 107
def play_queue
  song = playlists[:queue].remove_next
  play_in_player song
  return song
end
player_object(name) click to toggle source

Try to find the player object by name.

@param name [Symbol] The name of the player object @return [Object] The player object found, if any.

# File lib/cultome_player/player/interface/helper.rb, line 154
def player_object(name)
  case name
    when :song then playlists[:current].current
    else raise 'unknown player object:unknown player object'
  end
end
search_songs_with(cmd) click to toggle source

Search in library for songs that fullfil the command parameters.

@param cmd [Command] The user command. @return [List<Song>] The list of songs found.

# File lib/cultome_player/player/interface/helper.rb, line 129
def search_songs_with(cmd)
  criteria_query, criteria_values = process_for_search(cmd.params(:criteria))
  literal_query, literal_values = process_for_search(cmd.params(:literal))
  object_query, object_values = process_for_search(cmd.params(:object))
  # preparamos la query completa con sus parametros
  search_query = [criteria_query, object_query, literal_query].compact.collect{|q| "(#{q})" }.join(" or ")
  search_values = [criteria_values, object_values, literal_values].flatten.compact
  # hacemos la query!
  return search_query.empty? ? [] : Song.includes(:artist, :album).connected.where(search_query, *search_values).references(:artist, :album).to_a
end
select_songs_with(cmd) click to toggle source

Select songs from the library and current and focus playlist.

@param cmd [Command] The user command. @return [List<Song>] The list of songs picked.

# File lib/cultome_player/player/interface/helper.rb, line 117
def select_songs_with(cmd)
  found_songs = search_songs_with(cmd)
  from_focus = get_from_focus(cmd.params(:number))
  from_playlists = get_from_playlists(cmd.params_values(:object))
  results = found_songs + from_focus + from_playlists
  return results.uniq{|s| s.id }
end
update_song(new_info) click to toggle source

Updates a song into database

@param new_info [List<Hash>] Has contains the keys :artist_id, :album_id, :drive_id, :relative_path, :library_path (optional). @return [Integer] The number of songs updated.

# File lib/cultome_player/player/interface/helper.rb, line 84
def update_song(new_info)
  existing_paths = get_unique_paths
  to_be_processed = new_info.select{|s| existing_paths.include?(s[:file_path]) }
  progress = progress_label("Updating song {actual} of {total}", to_be_processed.size)
  return to_be_processed.count do |info|
    # extraemos la cancion almacenada.. si existe
    display_over c15(progress.increment)
    song = Song.includes(:drive).where("drives.path||'/'||songs.relative_path = ?", info[:file_path]).references(:drives).first

    song.nil? ? false : write_song(info, song)
  end
end
whole_library() click to toggle source

Extract the full list of songs connected.

@return [List<Song>] The full list of songs connected in library.

# File lib/cultome_player/player/interface/helper.rb, line 100
def whole_library
  Song.connected.to_a
end

Private Instance Methods

add_genre_to(song, genre) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 238
def add_genre_to(song, genre)
  unless genre.blank?
    song.genres << Genre.where(name: genre).first_or_create
  end
end
album_id(album_name) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 226
def album_id(album_name)
  return 0 if album_name.blank?
  album = Album.where(name: album_name).first_or_create
  return album.id
end
artist_id(artist_name) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 220
def artist_id(artist_name)
  return 0 if artist_name.blank?
  artist = Artist.where(name: artist_name).first_or_create
  return artist.id
end
drive_id(library_path) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 232
def drive_id(library_path)
  return 0 if library_path.blank?
  drive = Drive.where(path: library_path).first_or_create
  return drive.id
end
get_from_focus(params) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 210
def get_from_focus(params)
  params.map do |p|
    playlists[:focus].at p.value - 1
  end
end
get_less_played_criteria_limit() click to toggle source

Get the high limit for a “less played” songs criteria

# File lib/cultome_player/player/interface/helper.rb, line 342
def get_less_played_criteria_limit
  less_played_song_count = Song.all.order(:plays).limit(1).first.plays
  return less_played_song_count * 1.05
end
get_most_played_criteria_limit() click to toggle source

Get the lower limit for a “most played” songs criteria

# File lib/cultome_player/player/interface/helper.rb, line 348
def get_most_played_criteria_limit
  most_played_song_count = Song.all.order(plays: :desc).limit(1).first.plays
  return most_played_song_count * 0.95
end
get_recently_added_criteria_limit() click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 353
def get_recently_added_criteria_limit
  last_song_added_dt = Song.all.order(created_at: :desc).limit(1).first.created_at
  return last_song_added_dt - 1.day, last_song_added_dt
end
get_recently_played_criteria_limit() click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 358
def get_recently_played_criteria_limit
  last_song_played_dt = Song.all.order(last_played_at: :desc).limit(1).first.last_played_at
  return last_song_played_dt - 1.hour, last_song_played_dt
end
get_unique_paths() click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 216
def get_unique_paths
  Song.all.collect{|m| m.path }.uniq
end
increment() click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 203
def increment
  @_actual += 1
  return @_label.gsub(/\{actual\}/, @_actual.to_s)
end
progress_label(label, total) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 197
def progress_label(label, total)
  return Class.new do
    def initialize(lbl, tot)
      @_label, @_actual = lbl.gsub(/\{total\}/, tot.to_s), 0
    end

    def increment
      @_actual += 1
      return @_label.gsub(/\{actual\}/, @_actual.to_s)
    end
  end.new(label, total)
end
write_song(info, song=nil) click to toggle source
# File lib/cultome_player/player/interface/helper.rb, line 244
def write_song(info, song=nil)
  info[:artist_id] = artist_id(info[:artist])
  info[:album_id] = album_id(info[:album])
  info[:drive_id] = drive_id(info[:library_path])
  info[:relative_path] = info[:file_path].gsub("#{info[:library_path]}/", '')

  song_attr = info.select{|k,v| VALID_SONG_ATTR.include?(k) }

  if song.nil?
    song = Song.create!(song_attr)
  else
    song.update_attributes(song_attr)
  end

  add_genre_to(song, info[:genre])

  return song.persisted?
end