Class: Seq::Pager
Overview
Takes a list of items and returns them in pages. This can be thought of as doing the opposite of Paged.
Constant Summary
Constants inherited from Seq
Instance Method Summary collapse
-
#curr ⇒ Array
Returns the current page without modifying the current position.
-
#first ⇒ Array
Moves to the first page, and returns it.
-
#first! ⇒ Seq::Pager
Moves to the first page.
-
#first? ⇒ Boolean
Whether currently at the first page.
-
#initialize(page_size, start_page = nil, elements) ⇒ Pager
constructor
Creates a new instance of Pager.
-
#last ⇒ Array
Moves to the last page, and returns it.
-
#last! ⇒ Seq::Pager
Moves to the last page.
-
#last? ⇒ Boolean
Whether currently at the last page.
-
#next ⇒ Array?
Moves to, and returns the next page.
-
#next! ⇒ Seq::Pager
Moves the next page, and returns the Pager object itself.
-
#page ⇒ Integer
The index of the current page.
-
#page=(num) ⇒ Object
Sets the current page.
-
#pages ⇒ Integer
The total number of pages.
-
#prev ⇒ Array?
Moves to, and returns, the previous page.
-
#prev! ⇒ Seq::Pager
Moves to the previous page, and returns the Pager object itself.
-
#range(left, middle, right) ⇒ Object
Provides a list of page numbers, useful for populating a page selector.
Methods inherited from Seq
#each, #ended?, #entries, #inc, #infinite?, #method_missing, #reset, #to_a
Constructor Details
#initialize(page_size, start_page = nil, elements) ⇒ Pager
Creates a new instance of Pager.
26 27 28 29 |
# File 'lib/seq/pager.rb', line 26 def initialize(page_size, start_page=nil, elements) @pages = elements.each_slice(page_size).to_a @curr = start_page || 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Seq
Instance Method Details
#curr ⇒ Array
Returns the current page without modifying the current position.
101 102 103 |
# File 'lib/seq/pager.rb', line 101 def curr @pages[@curr] end |
#first ⇒ Array
Moves to the first page, and returns it.
73 74 75 76 |
# File 'lib/seq/pager.rb', line 73 def first @curr = 0 curr end |
#first! ⇒ Seq::Pager
Moves to the first page.
65 66 67 68 |
# File 'lib/seq/pager.rb', line 65 def first! @curr = 0 self end |
#first? ⇒ Boolean
Returns Whether currently at the first page.
58 59 60 |
# File 'lib/seq/pager.rb', line 58 def first? page == 0 end |
#last ⇒ Array
Moves to the last page, and returns it.
94 95 96 97 |
# File 'lib/seq/pager.rb', line 94 def last @curr = pages - 1 curr end |
#last! ⇒ Seq::Pager
Moves to the last page.
86 87 88 89 |
# File 'lib/seq/pager.rb', line 86 def last! @curr = 0 self end |
#last? ⇒ Boolean
Returns Whether currently at the last page.
79 80 81 |
# File 'lib/seq/pager.rb', line 79 def last? page == pages - 1 end |
#next ⇒ Array?
Moves to, and returns the next page. If at the last page it returns nil
.
117 118 119 120 121 122 |
# File 'lib/seq/pager.rb', line 117 def next return if last? @curr += 1 curr end |
#next! ⇒ Seq::Pager
Moves the next page, and returns the Seq::Pager object itself. If at the last page it simply returns self
.
109 110 111 112 |
# File 'lib/seq/pager.rb', line 109 def next! @curr += 1 unless last? self end |
#page ⇒ Integer
Returns The index of the current page.
48 49 50 |
# File 'lib/seq/pager.rb', line 48 def page @curr end |
#page=(num) ⇒ Object
Sets the current page. If the number passed is out of range, it will be set to within the bound of available pages.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/seq/pager.rb', line 35 def page=(num) unless num.respond_to?(:to_i) raise ArgumentError.new("cannot call #to_i on argument") end num = num.to_i num = 0 if num < 0 num = pages - 1 if num >= pages @curr = num end |
#pages ⇒ Integer
Returns The total number of pages.
53 54 55 |
# File 'lib/seq/pager.rb', line 53 def pages @pages.size end |
#prev ⇒ Array?
Moves to, and returns, the previous page. If at the first page it returns nil
.
137 138 139 140 141 142 |
# File 'lib/seq/pager.rb', line 137 def prev return if first? @curr -= 1 curr end |
#prev! ⇒ Seq::Pager
Moves to the previous page, and returns the Seq::Pager object itself. If at the first page it simply returns self
.
128 129 130 131 |
# File 'lib/seq/pager.rb', line 128 def prev! @curr -= 1 unless first? self end |
#range(left, middle, right) ⇒ Object
Provides a list of page numbers, useful for populating a page selector. It returns an array containing three arrays, some of which may be empty.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/seq/pager.rb', line 163 def range(left, middle, right) width = 2 * middle + 1 if pages < left + width + right return [(0..pages-1).to_a, [], []] end if page < left + middle + 1 left = (0..left+width-1).to_a middle = [] right = (pages-right..pages-1).to_a elsif page > (pages - 1) - right - middle - 1 left = (0..left-1).to_a middle = [] right = (pages-right-width..pages-1).to_a else left = (0..left-1).to_a middle = (page-middle..page+middle).to_a right = (pages-right..pages-1).to_a end [left, middle, right] end |