Class: PageIterator
- Inherits:
-
Object
- Object
- PageIterator
- Defined in:
- lib/page_iterator.rb
Constant Summary collapse
- DEFAULT_ITEMS_PER_PAGE =
50
Instance Attribute Summary collapse
-
#total_items ⇒ Object
readonly
Returns the value of attribute total_items.
Instance Method Summary collapse
- #each_remaining_page! ⇒ Object
-
#initialize(total_items, logfile, items_per_page = DEFAULT_ITEMS_PER_PAGE) ⇒ PageIterator
constructor
A new instance of PageIterator.
- #next! ⇒ Object
- #per_page ⇒ Object
- #previous! ⇒ Object
- #remaining_items ⇒ Object
- #remaining_pages ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(total_items, logfile, items_per_page = DEFAULT_ITEMS_PER_PAGE) ⇒ PageIterator
Returns a new instance of PageIterator.
6 7 8 9 10 11 |
# File 'lib/page_iterator.rb', line 6 def initialize(total_items, logfile, items_per_page=DEFAULT_ITEMS_PER_PAGE) @total_items = total_items @items_per_page = items_per_page @logfile = logfile @current_page = current_page_from_file end |
Instance Attribute Details
#total_items ⇒ Object (readonly)
Returns the value of attribute total_items.
4 5 6 |
# File 'lib/page_iterator.rb', line 4 def total_items @total_items end |
Instance Method Details
#each_remaining_page! ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/page_iterator.rb', line 49 def each_remaining_page! remaining_pages.each do |page| yield page self.next! end self.next! end |
#next! ⇒ Object
27 28 29 30 |
# File 'lib/page_iterator.rb', line 27 def next! @current_page += 1 log @current_page end |
#per_page ⇒ Object
13 14 15 |
# File 'lib/page_iterator.rb', line 13 def per_page @items_per_page end |
#previous! ⇒ Object
32 33 34 35 |
# File 'lib/page_iterator.rb', line 32 def previous! @current_page -= 1 log @current_page end |
#remaining_items ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/page_iterator.rb', line 37 def remaining_items if @current_page > total_pages 0 else @total_items - (@current_page - 1) * @items_per_page end end |
#remaining_pages ⇒ Object
45 46 47 |
# File 'lib/page_iterator.rb', line 45 def remaining_pages @current_page..total_pages end |
#total_pages ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/page_iterator.rb', line 17 def total_pages pages = @total_items / @items_per_page if pages == 0 1 else last_page_items = @total_items % @items_per_page last_page_items > 0 ? pages + 1 : pages end end |