Class: Dimples::Pagination::Pager
- Inherits:
-
Object
- Object
- Dimples::Pagination::Pager
- Includes:
- Enumerable
- Defined in:
- lib/dimples/pagination.rb
Overview
A class that models the context of a single page during pagination.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#item_count ⇒ Object
readonly
Returns the value of attribute item_count.
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#page_count ⇒ Object
readonly
Returns the value of attribute page_count.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(url, items, per_page, options = {}) ⇒ Pager
constructor
A new instance of Pager.
- #items_at(page) ⇒ Object
- #next_page_url ⇒ Object
- #previous_page_url ⇒ Object
- #step_to(page) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(url, items, per_page, options = {}) ⇒ Pager
Returns a new instance of Pager.
44 45 46 47 48 49 50 51 52 |
# File 'lib/dimples/pagination.rb', line 44 def initialize(url, items, per_page, = {}) @url = url @items = items @per_page = per_page @page_count = (items.length.to_f / per_page.to_i).ceil @page_prefix = [:page_prefix] || 'page' step_to(1) end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
38 39 40 |
# File 'lib/dimples/pagination.rb', line 38 def current_page @current_page end |
#item_count ⇒ Object (readonly)
Returns the value of attribute item_count.
42 43 44 |
# File 'lib/dimples/pagination.rb', line 42 def item_count @item_count end |
#next_page ⇒ Object (readonly)
Returns the value of attribute next_page.
40 41 42 |
# File 'lib/dimples/pagination.rb', line 40 def next_page @next_page end |
#page_count ⇒ Object (readonly)
Returns the value of attribute page_count.
41 42 43 |
# File 'lib/dimples/pagination.rb', line 41 def page_count @page_count end |
#previous_page ⇒ Object (readonly)
Returns the value of attribute previous_page.
39 40 41 |
# File 'lib/dimples/pagination.rb', line 39 def previous_page @previous_page end |
Instance Method Details
#each(&block) ⇒ Object
54 55 56 57 58 |
# File 'lib/dimples/pagination.rb', line 54 def each(&block) (1..@page_count).each do |index| block.yield step_to(index), items_at(index) end end |
#items_at(page) ⇒ Object
68 69 70 |
# File 'lib/dimples/pagination.rb', line 68 def items_at(page) @items.slice((page - 1) * @per_page, @per_page) end |
#next_page_url ⇒ Object
77 78 79 |
# File 'lib/dimples/pagination.rb', line 77 def next_page_url "#{@url}#{@page_prefix}#{@next_page}" if @next_page end |
#previous_page_url ⇒ Object
72 73 74 75 |
# File 'lib/dimples/pagination.rb', line 72 def previous_page_url return unless @previous_page @previous_page != 1 ? "#{@url}#{@page_prefix}#{@previous_page}" : @url end |
#step_to(page) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/dimples/pagination.rb', line 60 def step_to(page) @current_page = (1..@page_count).cover?(page) ? page : 1 @previous_page = (@current_page - 1).positive? ? @current_page - 1 : nil @next_page = @current_page + 1 <= @page_count ? @current_page + 1 : nil @current_page end |
#to_h ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dimples/pagination.rb', line 81 def to_h output = { current_page: @current_page, page_count: @page_count, item_count: @items.count, url: @url } if @previous_page output[:previous_page] = @previous_page output[:previous_page_url] = previous_page_url end if @next_page output[:next_page] = @next_page output[:next_page_url] = next_page_url end output end |