class Airspace::Chunker

Chunking here is defined as: taking an array of pages and grouping them into groups of pages (chunks) in order to find a middle-ground of server-side page and entire dataset fetches.

Constants

Chunk
Location

Attributes

pages_per_chunk[R]

Public Class Methods

new(pages_per_chunk) click to toggle source
# File lib/airspace/chunker.rb, line 19
def initialize(pages_per_chunk)
  raise ArgumentError unless pages_per_chunk.positive?

  @pages_per_chunk = pages_per_chunk
end

Public Instance Methods

count(page_total) click to toggle source
# File lib/airspace/chunker.rb, line 25
def count(page_total)
  (page_total / pages_per_chunk.to_f).ceil
end
each(page_total) { |chunk| ... } click to toggle source
# File lib/airspace/chunker.rb, line 29
def each(page_total)
  return enum_for(:each, page_total) unless block_given?

  (0...count(page_total)).each do |chunk_index|
    page_index_start = chunk_index * pages_per_chunk
    page_index_end   = page_index_start + pages_per_chunk - 1

    yield Chunk.new(chunk_index, page_index_start, page_index_end)
  end
end
locate(index) click to toggle source
# File lib/airspace/chunker.rb, line 40
def locate(index)
  chunk_index = (index / pages_per_chunk.to_f).floor
  page_index = index % pages_per_chunk

  Location.new(chunk_index, page_index)
end