class Nehm::TracksViewCommand

TracksViewCommand is a super class for all commands, that show tracks in list and let download selected tracks. When creating a new TracksViewCommand, define get_tracks (protected), initialize, arguments, program_name, summary and usage. You can also define execute if you need, but you must place also ‘super’ in this inherited method

See ‘list_command.rb’ or ‘search_command.rb’ as example

Constants

DEFAULT_LIMIT
DEFAULT_OFFSET

Public Class Methods

new() click to toggle source
Calls superclass method Nehm::Command::new
# File lib/nehm/tracks_view_command.rb, line 18
def initialize
  super
end

Public Instance Methods

execute() click to toggle source
# File lib/nehm/tracks_view_command.rb, line 22
def execute
  setup_environment

  old_offset = @offset

  @queue = []
  @track_manager = TrackManager.new(@options)

  tracks = get_tracks
  UI.term 'There are no tracks yet' if tracks.nil?

  loop do
    # If offset changed, update list of tracks
    unless old_offset == @offset
      tracks = get_tracks
      old_offset = @offset
    end

    if tracks.nil?
      prev_page
      next
    end

    show_menu(tracks)
  end
end

Protected Instance Methods

add_track_to_queue(track) click to toggle source
# File lib/nehm/tracks_view_command.rb, line 84
def add_track_to_queue(track)
  @queue << track
end
download_tracks_from_queue() click to toggle source
# File lib/nehm/tracks_view_command.rb, line 88
def download_tracks_from_queue
  UI.newline
  @track_manager.process_tracks(@queue)
end
get_tracks() click to toggle source
# File lib/nehm/tracks_view_command.rb, line 51
def get_tracks
  raise StandardError, "You must define 'get_tracks' method"
end
next_page() click to toggle source
# File lib/nehm/tracks_view_command.rb, line 93
def next_page
  @offset += @limit
end
prev_page() click to toggle source
# File lib/nehm/tracks_view_command.rb, line 97
def prev_page
  @offset -= @limit if @offset >= @limit
end
setup_environment() click to toggle source
# File lib/nehm/tracks_view_command.rb, line 79
def setup_environment
  @limit = @options[:limit] ? @options[:limit].to_i : DEFAULT_LIMIT
  @offset = @options[:offset] ? @options[:offset].to_i : DEFAULT_OFFSET
end
show_menu(tracks) click to toggle source
# File lib/nehm/tracks_view_command.rb, line 56
def show_menu(tracks)
  UI.menu do |menu|
    menu.header = 'Enter the number of track to add it to download queue'.green

    ids = @queue.map(&:id) # Get ids of tracks in queue
    tracks.each do |track|
      track_info = "#{track.full_name} | #{track.duration}"

      if ids.include? track.id
        menu.choice(:added, track_info)
      else
        menu.choice(:inc, track_info) { add_track_to_queue(track) }
      end
    end

    menu.newline

    menu.choice('d', 'Download tracks from queue'.green.freeze) { download_tracks_from_queue; UI.term }
    menu.choice('n', 'Next page'.magenta.freeze) { next_page }
    menu.choice('p', 'Prev page'.magenta.freeze) { prev_page }
  end
end