# -*- coding: utf-8 -*- module JustPaginate VERSION = "0.0.12" # TODO make sure negative numbers, non-integers etc are just converted to page 1. def self.page_value(page) if page.nil? 1 else page.to_i end end def self.paginate(curr_page, per_page, total_entry_count, &selection_strategy) raise "Pagination just supplies index range, expects a selection strategy" if selection_strategy.nil? entries = yield(index_range(curr_page, per_page, total_entry_count)) || [] return entries, total_page_number(total_entry_count, per_page) end def self.total_page_number(total_entry_count, per_page) (total_entry_count.to_f / per_page).ceil end def self.index_range(curr_page, per_page, total_entry_count) start_index = ((curr_page-1)*per_page) end_index = (start_index+per_page)-1 if(start_index>(total_entry_count-1)) start_index = total_entry_count-per_page end_index = total_entry_count-1 end if end_index>total_entry_count end_index = total_entry_count end Range.new(start_index, end_index) end def self.beyond_page_range?(curr_page, per_page, total_entry_count) if curr_page < 1 true else start_index = ((curr_page-1)*per_page) end_index = (start_index+per_page)-1 start_index > total_entry_count end end def self.page_navigation(curr_page, total_page_count, &page_link_constructor) links = page_links(curr_page.to_i, total_page_count, &page_link_constructor) return "