class Shamu::Entities::PagedList

A list of {Entities::Entity} records.

Public Class Methods

new( entities, total_count: :not_set, limit: :not_set, offset: :not_set, has_next: :not_set, has_previous: :not_set ) click to toggle source

@param [Enumerable] entities the raw list of entities. @param [Integer, call] total_count the number (or a proc that resolves to a number) of records in the entire set. @param [Integer, call] limit the maximum number (or a proc that resolves to a number) of records in the page represented by the list. @param [Integer, call] offset the number (or a proc that resolves to a number) offset from the start of the set that this list represents. @param [Boolean,#call] has_next true if there is another page available or a proc that returns a bool. @param [Boolean,#call] has_previous true if there is a previous page available or a proc that returns a bool.

Calls superclass method Shamu::Entities::List::new
# File lib/shamu/entities/paged_list.rb, line 19
def initialize( entities,
                total_count: :not_set,
                limit: :not_set,
                offset: :not_set,
                has_next: :not_set,
                has_previous: :not_set )
  super( entities )

  @total_count  = total_count
  @limit        = limit
  @offset       = offset
  @has_next     = has_next
  @has_previous = has_previous
end

Public Instance Methods

current_page() click to toggle source

@return [Integer] the current page number.

# File lib/shamu/entities/paged_list.rb, line 83
def current_page
  if limit > 0
    ( offset / limit ).to_i + 1
  else
    1
  end
end
first?() click to toggle source

@return [Boolean] true if this list represents the first page in the set.

# File lib/shamu/entities/paged_list.rb, line 132
def first?
  !previous?
end
has_next?()
Alias for: next?
has_prev?()
Alias for: previous?
has_previous()
Alias for: previous?
last?() click to toggle source

@return [Boolean] true if this list represents the last page in the set.

# File lib/shamu/entities/paged_list.rb, line 109
def last?
  !next?
end
limit() click to toggle source

@return [Integer] the maximum number of records to return in each page.

# File lib/shamu/entities/paged_list.rb, line 51
def limit
  if @limit == :not_set
    if raw_entities.respond_to?( :limit_value )
      raw_entities.limit_value
    elsif raw_entities.respond_to?( :limit )
      raw_entities.limit
    end
  elsif @limit.respond_to?( :call )
    @limit = @limit.call
  else
    @limit
  end
end
Also aliased as: per_page
next?() click to toggle source

@return [Boolean] true if there is another page of data available.

# File lib/shamu/entities/paged_list.rb, line 92
def next?
  if @has_next == :not_set
    if raw_entities.respond_to?( :has_next? )
      raw_entities.has_next?
    elsif raw_entities.respond_to?( :last_page? )
      !raw_entities.last_page?
    end
  elsif @has_next.respond_to?( :call )
    @has_next = @has_next.call
  else
    @has_next
  end
end
Also aliased as: has_next?
offset() click to toggle source

@return [Integer] the absolute offset into the set for the window of data that that this list contains.

# File lib/shamu/entities/paged_list.rb, line 68
def offset
  if @offset == :not_set
    if raw_entities.respond_to?( :offset_value )
      raw_entities.offset_value
    elsif raw_entities.respond_to?( :offset )
      raw_entities.offset
    end
  elsif @offset.respond_to?( :call )
    @offset = @offset.call
  else
    @offset
  end
end
paged?() click to toggle source

(see List#paged?)

# File lib/shamu/entities/paged_list.rb, line 35
def paged?
  true
end
per_page()
Alias for: limit
previous?() click to toggle source

@return [Boolean] true if there is another page of data available.

# File lib/shamu/entities/paged_list.rb, line 114
def previous?
  if @has_previous == :not_set
    if raw_entities.respond_to?( :has_previous? )
      raw_entities.has_previous?
    elsif raw_entities.respond_to?( :first_page? )
      !raw_entities.first_page?
    end
  elsif @has_previous.respond_to?( :call )
    @has_previous = @has_previous.call
  else
    @has_previous
  end
end
Also aliased as: has_prev?, has_previous
total_count() click to toggle source

@return [Integer] the total number of records in the set.

# File lib/shamu/entities/paged_list.rb, line 40
def total_count
  if @total_count == :not_set
    raw_entities.total_count
  elsif @total_count.respond_to?( :call )
    @total_count = @total_count.call
  else
    @total_count
  end
end