Module: CultomePlayer::Player::Interface::Helper
- Includes:
- Objects
- Included in:
- CultomePlayer::Player::Interface, Extended
- Defined in:
- lib/cultome_player/player/interface/helper.rb
Constant Summary collapse
- VALID_SONG_ATTR =
[:name, :year, :track, :duration, :relative_path, :artist_id, :album_id, :drive_id]
Instance Method Summary collapse
-
#connect_response_msg(imported, updated) ⇒ String
Select the connect action message depending on the imported and updated parameters.
-
#format_secs(secs) ⇒ String
Returns a string representation of a number of seconds in the format: mm:ss.
-
#get_files_in_tree(path, *extensions) ⇒ List<String>
Return a list of absolute path of files in the path which has extension.
-
#get_from_playlists(lists) ⇒ List<Song>
Get a list of songs from selected playlists, only if the playlist exist.
-
#get_progress_bar(current, total = 100, size = 10) ⇒ String
Returns a representation of a progress bar.
-
#get_progress_bar_with_labels(current, total = 100, size = 10, left = '', right = '') ⇒ String
Returns a representation of a progress bar.
-
#insert_song(new_info) ⇒ Integer
Insert a new song into database.
-
#play_inline?(cmd) ⇒ Boolean
Check if a command has the format to be considered a ply inline (dont create a playlist).
-
#play_queue ⇒ Song
Play the next song in queue playlist.
-
#player_object(name) ⇒ Object
Try to find the player object by name.
-
#process_for_search(params) ⇒ (List<String>,List<String>)
Generate a query and a array of values to replace into the query for a given set of parameters.
-
#search_songs_with(cmd) ⇒ List<Song>
Search in library for songs that fullfil the command parameters.
-
#select_songs_with(cmd) ⇒ List<Song>
Select songs from the library and current and focus playlist.
-
#update_song(new_info) ⇒ Integer
Updates a song into database.
-
#whole_library ⇒ List<Song>
Extract the full list of songs connected.
Instance Method Details
#connect_response_msg(imported, updated) ⇒ String
Select the connect action message depending on the imported and updated parameters.
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/cultome_player/player/interface/helper.rb', line 182 def connect_response_msg(imported, updated) = "" if imported > 0 += "Songs imported: #{imported}." end if updated > 0 += "Songs updated: #{updated}." end return end |
#format_secs(secs) ⇒ String
Returns a string representation of a number of seconds in the format: mm:ss
11 12 13 14 15 |
# 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) ⇒ List<String>
Return a list of absolute path of files in the path which has extension.
60 61 62 63 64 |
# 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) ⇒ List<Song>
Get a list of songs from selected playlists, only if the playlist exist.
144 145 146 147 148 |
# 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) ⇒ String
Returns a representation of a progress bar.
32 33 34 35 36 37 38 |
# File 'lib/cultome_player/player/interface/helper.rb', line 32 def (current, total=100, size=10) factor = total > 0 ? current / total.to_f : 0 = ( factor * size ).floor total = "-" * size total[0,] = "#" * return "|#{total}>" end |
#get_progress_bar_with_labels(current, total = 100, size = 10, left = '', right = '') ⇒ String
Returns a representation of a progress bar.
26 27 28 29 |
# File 'lib/cultome_player/player/interface/helper.rb', line 26 def (current, total=100, size=10, left='', right='') = (current, total, size) return "#{left} #{} #{right}".strip end |
#insert_song(new_info) ⇒ Integer
Insert a new song into database. Except if its already present by path.
70 71 72 73 74 75 76 77 78 |
# 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) ⇒ Boolean
Check if a command has the format to be considered a ply inline (dont create a playlist).
165 166 167 168 169 170 171 172 173 174 175 |
# 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 ⇒ Song
Play the next song in queue playlist.
107 108 109 110 111 |
# 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) ⇒ Object
Try to find the player object by name.
154 155 156 157 158 159 |
# 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 |
#process_for_search(params) ⇒ (List<String>,List<String>)
Generate a query and a array of values to replace into the query for a given set of parameters.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cultome_player/player/interface/helper.rb', line 44 def process_for_search(params) return nil, [] if params.empty? query, values = case params.first.type when :object then process_object_for_search(params) when :criteria then process_criteria_for_search(params) when :literal then process_literal_for_search(params) end return query, values end |
#search_songs_with(cmd) ⇒ List<Song>
Search in library for songs that fullfil the command parameters.
129 130 131 132 133 134 135 136 137 138 |
# 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) ⇒ List<Song>
Select songs from the library and current and focus playlist.
117 118 119 120 121 122 123 |
# 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) ⇒ Integer
Updates a song into database
84 85 86 87 88 89 90 91 92 93 94 95 |
# 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 |