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

Instance Method Details

#connect_response_msg(imported, updated) ⇒ String

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

Parameters:

  • imported (Integer)

    The number of imported songs.

  • updated (Integer)

    The number of updated songs.

Returns:

  • (String)

    The appropiated message to show to user.



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)
  message = ""
  if imported > 0
    message += "Songs imported: #{imported}."
  end

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

  return message
end

#format_secs(secs) ⇒ String

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

Parameters:

  • secs (Integer)

    Number of seconds to be represented.

Returns:

  • (String)

    The number of seconds formatted as 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.

Parameters:

  • path (String)

    The path to searlook for files.

  • extension (List<String>)

    The list of extension to filter the files with.

Returns:

  • (List<String>)

    The absolute paths to the files found.



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.

Parameters:

  • lists (List<Symbol>)

    The names of the playlists to check.

Returns:

  • (List<Song>)

    The songs in the valid playlists.



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.

Parameters:

  • current (Integer)

    The actual progress.

  • total (Integer) (defaults to: 100)

    The total progress to achive.

  • total (Integer) (defaults to: 100)

    Optional, the total progress to achive. Default 100.

  • size (Integer) (defaults to: 10)

    Optional, the width of the bar. Default 10.

  • left (String)

    Optional, prefix to append. Default ”.

  • right (String)

    Optional, postfix to append. Default ”.

Returns:

  • (String)

    The string representation of a progress bar.



32
33
34
35
36
37
38
# 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 = '') ⇒ String

Returns a representation of a progress bar.

Parameters:

  • current (Integer)

    The actual progress.

  • total (Integer) (defaults to: 100)

    The total progress to achive.

  • total (Integer) (defaults to: 100)

    Optional, the total progress to achive. Default 100.

  • size (Integer) (defaults to: 10)

    Optional, the width of the bar. Default 10.

  • left (String) (defaults to: '')

    Optional, prefix to append. Default ”.

  • right (String) (defaults to: '')

    Optional, postfix to append. Default ”.

Returns:

  • (String)

    The string representation of a progress bar.



26
27
28
29
# 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) ⇒ Integer

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

Parameters:

  • new_info (List<Hash>)

    Has contains the keys :artist_id, :album_id, :drive_id, :relative_path, :library_path (optional).

Returns:

  • (Integer)

    The number of songs writed.



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

Parameters:

  • cmd (Command)

    The command to check.

Returns:

  • (Boolean)

    True if is considered to be played inline. False otherwise.



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_queueSong

Play the next song in queue playlist.

Returns:

  • (Song)

    The song programed.



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.

Parameters:

  • name (Symbol)

    The name of the player object

Returns:

  • (Object)

    The player object found, if any.



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.

Parameters:

  • params (List<Parameter>)

    The list of serach parameter to prepare.

Returns:

  • ((List<String>,List<String>))

    The query and the values set to use.



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.

Parameters:

  • cmd (Command)

    The user command.

Returns:

  • (List<Song>)

    The list of songs found.



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.

Parameters:

  • cmd (Command)

    The user command.

Returns:

  • (List<Song>)

    The list of songs picked.



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

Parameters:

  • new_info (List<Hash>)

    Has contains the keys :artist_id, :album_id, :drive_id, :relative_path, :library_path (optional).

Returns:

  • (Integer)

    The number of songs updated.



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

#whole_libraryList<Song>

Extract the full list of songs connected.

Returns:

  • (List<Song>)

    The full list of songs connected in library.



100
101
102
# File 'lib/cultome_player/player/interface/helper.rb', line 100

def whole_library
  Song.connected.to_a
end