Skip to content

Instantly share code, notes, and snippets.

@levand
Created May 5, 2011 18:18
Show Gist options
  • Save levand/957573 to your computer and use it in GitHub Desktop.
Save levand/957573 to your computer and use it in GitHub Desktop.
def page_map(page, count, vidaas_count = 20)
  start_idx = page * count
  first_page = start_idx / vidaas_count
  last_page = first_page + (count / vidaas_count)
  start_subset = start_idx % vidaas_count
  end_subset = start_subset + count
  return {
    :count => count > vidaas_count ? vidaas_count : count,
    :pages => (first_page..last_page),
    :indexes => (start_subset...end_subset)
  }
end
# Mock vidaas data set and query method
DATA_SET = (0...100).to_a
def query(page, count)
  start = (page - 1) * count
  DATA_SET[start...(start + count)]
end
# Our actual search that uses page_map to adjust for the vidaas limit
def search(req_page, req_count)
  results = []
  adjusted = page_map(req_page, req_count)
  adjusted[:pages].each do |page|
    results += query(page+1, adjusted[:count])
  end
  results[adjusted[:indexes]]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment