Class: AppQuery::Paginatable::PaginatedResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/app_query/paginatable.rb

Overview

Kaminari-compatible wrapper for paginated results.

Wraps an array of records with pagination metadata, providing a consistent interface for both counted and uncounted pagination modes.

Includes Enumerable, so all standard iteration methods work directly.

Examples:

result = ArticlesQuery.new.paginate(page: 2).entries
result.each { |article| puts article["title"] }
result.current_page # => 2
result.total_pages  # => 5

Instance Method Summary collapse

Constructor Details

#initialize(records, page:, per_page:, total_count: nil, has_next: nil) ⇒ PaginatedResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PaginatedResult.



66
67
68
69
70
71
72
# File 'lib/app_query/paginatable.rb', line 66

def initialize(records, page:, per_page:, total_count: nil, has_next: nil)
  @records = records
  @page = page
  @per_page = per_page
  @total_count = total_count
  @has_next = has_next
end

Instance Method Details

#current_pageInteger

Returns the current page number.

Returns:

  • (Integer)

    the current page number



75
# File 'lib/app_query/paginatable.rb', line 75

def current_page = @page

#first_page?Boolean

Returns true if this is the first page.

Returns:

  • (Boolean)

    true if this is the first page



84
# File 'lib/app_query/paginatable.rb', line 84

def first_page? = @page == 1

#last_page?Boolean

Returns true if this is the last page.

Returns:

  • (Boolean)

    true if this is the last page



108
109
110
111
112
113
114
# File 'lib/app_query/paginatable.rb', line 108

def last_page?
  if @total_count
    @page >= total_pages
  else
    !@has_next
  end
end

#limit_valueInteger

Returns the number of records per page.

Returns:

  • (Integer)

    the number of records per page



78
# File 'lib/app_query/paginatable.rb', line 78

def limit_value = @per_page

#next_pageInteger?

Returns the next page number, or nil if on last page.

Returns:

  • (Integer, nil)

    the next page number, or nil if on last page



99
100
101
102
103
104
105
# File 'lib/app_query/paginatable.rb', line 99

def next_page
  if @total_count
    (@page < total_pages) ? @page + 1 : nil
  else
    @has_next ? @page + 1 : nil
  end
end

#out_of_range?Boolean

Returns true if the requested page is beyond available data.

Returns:

  • (Boolean)

    true if the requested page is beyond available data



117
118
119
# File 'lib/app_query/paginatable.rb', line 117

def out_of_range?
  empty? && @page > 1
end

#prev_pageInteger?

Returns the previous page number, or nil if on first page.

Returns:

  • (Integer, nil)

    the previous page number, or nil if on first page



81
# File 'lib/app_query/paginatable.rb', line 81

def prev_page = (@page > 1) ? @page - 1 : nil

#total_countInteger

Returns the total number of records across all pages.

Returns:

  • (Integer)

    the total number of records across all pages

Raises:

  • (RuntimeError)

    if called in +without_count+ mode



88
89
90
# File 'lib/app_query/paginatable.rb', line 88

def total_count
  @total_count || raise("total_count not available in without_count mode")
end

#total_pagesInteger?

Returns the total number of pages, or nil in +without_count+ mode.

Returns:

  • (Integer, nil)

    the total number of pages, or nil in +without_count+ mode



93
94
95
96
# File 'lib/app_query/paginatable.rb', line 93

def total_pages
  return nil unless @total_count
  (@total_count.to_f / @per_page).ceil
end

#transform! {|record| ... } ⇒ self

Transforms each record in place using the given block.

Examples:

result.transform! { |row| OpenStruct.new(row) }

Yields:

  • (record)

    Block to transform each record

Yield Parameters:

  • record (Hash)

    the record to transform

Yield Returns:

  • (Object)

    the transformed record

Returns:

  • (self)

    for chaining



130
131
132
133
# File 'lib/app_query/paginatable.rb', line 130

def transform!
  @records = @records.map { |r| yield(r) }
  self
end