class NeatPages::Base
*************************************************************************************
TOCOMMENT
*************************************************************************************
Attributes
current_page[R]
per_page[RW]
total_items[R]
Public Class Methods
new(current_page_param, options={})
click to toggle source
*************************************************************************************
CONSTRUCTOR
*************************************************************************************
# File lib/neat_pages/base.rb, line 13 def initialize(current_page_param, options={}) options = { per_page: 10, total_items: 0 }.merge(options) @per_page = options[:per_page].to_i @total_items = options[:total_items] init_current_page current_page_param @out_of_bound = init_out_of_bound end
Public Instance Methods
empty?()
click to toggle source
*************************************************************************************
PUBLIC INSTANCE METHODS
*************************************************************************************
# File lib/neat_pages/base.rb, line 28 def empty? @total_items == 0 end
limit()
click to toggle source
# File lib/neat_pages/base.rb, line 32 def limit @per_page end
next?()
click to toggle source
# File lib/neat_pages/base.rb, line 40 def next? @current_page < total_pages and not out_of_bound? end
next_page()
click to toggle source
# File lib/neat_pages/base.rb, line 36 def next_page next? ? (@current_page + 1) : total_pages end
offset()
click to toggle source
# File lib/neat_pages/base.rb, line 44 def offset if out_of_bound? total_items + 1 else (@current_page - 1) * @per_page end end
out_of_bound?()
click to toggle source
# File lib/neat_pages/base.rb, line 52 def out_of_bound? @out_of_bound end
paginated?()
click to toggle source
# File lib/neat_pages/base.rb, line 56 def paginated? @total_items > @per_page end
previous?()
click to toggle source
# File lib/neat_pages/base.rb, line 64 def previous? @current_page > 1 and not out_of_bound? end
previous_page()
click to toggle source
# File lib/neat_pages/base.rb, line 60 def previous_page previous? ? (@current_page - 1) : 1 end
response_headers()
click to toggle source
# File lib/neat_pages/base.rb, line 68 def response_headers { "X-Total-Items" => @total_items.to_s, "X-Total-Pages" => total_pages.to_s, "X-Per-Page" => @per_page.to_s, "X-Current-Page" => @current_page.to_s } end
set_total_items(qte)
click to toggle source
# File lib/neat_pages/base.rb, line 77 def set_total_items(qte) @total_items = qte @out_of_bound = init_out_of_bound end
total_pages()
click to toggle source
# File lib/neat_pages/base.rb, line 82 def total_pages total = (@total_items.to_f / @per_page).ceil total != 0 ? total : 1 end
Private Instance Methods
init_current_page(current_page_param)
click to toggle source
*************************************************************************************
PRIVATE INSTANCE METHODS
*************************************************************************************
# File lib/neat_pages/base.rb, line 92 def init_current_page(current_page_param) @current_page = current_page_param.to_i @current_page = 1 if @current_page == 0 end
init_out_of_bound()
click to toggle source
# File lib/neat_pages/base.rb, line 97 def init_out_of_bound @current_page <= 0 or @current_page > total_pages end