Class: Webby::Paginator
- Inherits:
-
Object
- Object
- Webby::Paginator
- Includes:
- Enumerable
- Defined in:
- lib/webby/stelan/paginator.rb
Defined Under Namespace
Classes: ArgumentError, MissingCountError, MissingSelectError, Page
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #each ⇒ Object
-
#first ⇒ Object
First page object.
-
#initialize(count, per_page, resource, &select) ⇒ Paginator
constructor
Instantiate a new Paginator object.
-
#last ⇒ Object
Last page object.
-
#number_of_pages ⇒ Object
Total number of pages.
-
#page(number) ⇒ Object
Retrieve page object by number.
-
#reset ⇒ Object
Finalizer method that should be called when the paginator is finished.
Methods included from Enumerable
Constructor Details
#initialize(count, per_page, resource, &select) ⇒ Paginator
Instantiate a new Paginator object
Provide:
-
A total count of the number of objects to paginate
-
The number of objects in each page
-
A block that returns the array of items
-
The block is passed the item offset (and the number of items to show per page, for convenience, if the arity is 2)
-
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/webby/stelan/paginator.rb', line 32 def initialize(count, per_page, resource, &select) @count, @per_page, @resource = count, per_page, resource @meta_data = @resource..dup @filename = @resource.filename @directory = @resource.directory unless select raise MissingSelectError, "Must provide block to select data for each page" end @select = select end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
21 22 23 |
# File 'lib/webby/stelan/paginator.rb', line 21 def count @count end |
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
21 22 23 |
# File 'lib/webby/stelan/paginator.rb', line 21 def directory @directory end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
21 22 23 |
# File 'lib/webby/stelan/paginator.rb', line 21 def filename @filename end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
21 22 23 |
# File 'lib/webby/stelan/paginator.rb', line 21 def per_page @per_page end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
21 22 23 |
# File 'lib/webby/stelan/paginator.rb', line 21 def resource @resource end |
Instance Method Details
#each ⇒ Object
58 59 60 61 62 |
# File 'lib/webby/stelan/paginator.rb', line 58 def each 1.upto(number_of_pages) do |number| yield page(number) end end |
#first ⇒ Object
First page object
49 50 51 |
# File 'lib/webby/stelan/paginator.rb', line 49 def first page 1 end |
#last ⇒ Object
Last page object
54 55 56 |
# File 'lib/webby/stelan/paginator.rb', line 54 def last page number_of_pages end |
#number_of_pages ⇒ Object
Total number of pages
44 45 46 |
# File 'lib/webby/stelan/paginator.rb', line 44 def number_of_pages (@count / @per_page).to_i + (@count % @per_page > 0 ? 1 : 0) end |
#page(number) ⇒ Object
Retrieve page object by number
65 66 67 68 69 70 71 72 73 |
# File 'lib/webby/stelan/paginator.rb', line 65 def page(number) number = (n = number.to_i) > 0 ? n : 1 Page.new(self, number, lambda { offset = (number - 1) * @per_page args = [offset] args << @per_page if @select.arity == 2 @select.call(*args) }) end |
#reset ⇒ Object
Finalizer method that should be called when the paginator is finished
76 77 78 |
# File 'lib/webby/stelan/paginator.rb', line 76 def reset resource._reset(@meta_data) end |