class PagedGroups::Builder

This is the Public API for this library.

Constants

DEFAULT_PAGE_SIZE

Attributes

limit[R]
page_count[R]
page_size[R]
row_count[R]
space[R]
spacer[R]

Public Class Methods

new(limit: nil, page_size: DEFAULT_PAGE_SIZE, space: false, spacer: nil) click to toggle source
# File lib/paged_groups/builder.rb, line 22
def initialize(limit: nil, page_size: DEFAULT_PAGE_SIZE, space: false, spacer: nil)
  @limit      = limit ? limit.to_i : nil
  @page_size  = page_size ? page_size.to_i : DEFAULT_PAGE_SIZE
  @space      = space || false
  @spacer     = spacer

  clear
end

Public Instance Methods

add(groups, force: false) click to toggle source

Groups should be a two-dimensional array with the first dimension being the group and the second dimension being the record.

# File lib/paged_groups/builder.rb, line 33
def add(groups, force: false)
  dirty!

  groups.each do |group|
    limit_group_slice(Array(group)).each do |split_group|
      insert(split_group, force: force)
    end
  end

  self
end
all() click to toggle source
# File lib/paged_groups/builder.rb, line 54
def all
  return @all if @all

  @all = top? ? @pages : @pages + [@current_page]
end
Also aliased as: to_a
clear() click to toggle source
# File lib/paged_groups/builder.rb, line 45
def clear
  dirty!

  @pages        = []
  @page_count   = 0
  @current_page = []
  @row_count    = 0
end
to_a()
Alias for: all
to_s() click to toggle source
# File lib/paged_groups/builder.rb, line 61
def to_s
  "[#{self.class.name}] Page Count: #{page_count}, Row Count: #{row_count}"
end

Private Instance Methods

cut!() click to toggle source
# File lib/paged_groups/builder.rb, line 91
def cut!
  @page_count += 1
  @pages << @current_page
  @current_page = []

  nil
end
dirty!() click to toggle source
# File lib/paged_groups/builder.rb, line 87
def dirty!
  @all = nil
end
insert(rows, force: false) click to toggle source
# File lib/paged_groups/builder.rb, line 67
def insert(rows, force: false)
  cut! if start_new_page?(rows) && !force

  space_if_needed

  @current_page += rows

  @row_count += rows.length

  self
end
limit_group_slice(group) click to toggle source
# File lib/paged_groups/builder.rb, line 111
def limit_group_slice(group)
  if limit
    group.each_slice(limit)
  else
    [group]
  end
end
not_top?() click to toggle source
# File lib/paged_groups/builder.rb, line 79
def not_top?
  !top?
end
space_if_needed() click to toggle source
# File lib/paged_groups/builder.rb, line 99
def space_if_needed
  @current_page << spacer if space && not_top?

  nil
end
start_new_page?(next_items) click to toggle source
# File lib/paged_groups/builder.rb, line 105
def start_new_page?(next_items)
  proposed_current_page_length = @current_page.length + next_items.length

  not_top? && proposed_current_page_length > page_size
end
top?() click to toggle source
# File lib/paged_groups/builder.rb, line 83
def top?
  @current_page.empty?
end