Class: RailsConsolePro::Paginator

Inherits:
Object
  • Object
show all
Includes:
ColorHelper
Defined in:
lib/rails_console_pro/paginator.rb

Overview

Smart paginator for large collections with lazy enumeration Uses advanced Ruby concepts: Enumerator, lazy evaluation, and interactive I/O

Constant Summary collapse

COMMANDS =

Navigation commands

{
  next: ['n', 'next', ''],
  previous: ['p', 'prev', 'previous'],
  first: ['f', 'first'],
  last: ['l', 'last'],
  jump: ['j', 'jump', 'goto', 'g'],
  quit: ['q', 'quit', 'exit']
}.freeze

Instance Method Summary collapse

Methods included from ColorHelper

#bold_color, #color, #method_missing, #pastel, #respond_to_missing?

Constructor Details

#initialize(output, collection, total_count, config, record_printer_proc) ⇒ Paginator

Returns a new instance of Paginator.



19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_console_pro/paginator.rb', line 19

def initialize(output, collection, total_count, config, record_printer_proc)
  @output = output
  @collection = collection
  @total_count = total_count
  @config = config
  @record_printer_proc = record_printer_proc
  @page_size = config.pagination_page_size
  @current_page = 1
  @total_pages = calculate_total_pages
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsConsolePro::ColorHelper

Instance Method Details

#paginateObject

Start paginated display



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_console_pro/paginator.rb', line 31

def paginate
  return print_all if @total_count <= @config.pagination_threshold || !@config.pagination_enabled

  loop do
    print_page
    command = get_user_command
    break if command == :quit

    handle_command(command)
  end
end