Class: Thoth::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/thoth/helper/pagination.rb

Overview

The Pager class provides a simple wrapper around a paginated Sequel dataset.

Instance Method Summary collapse

Constructor Details

#initialize(dataset, url) ⇒ Pager

Initializes a new Pager instance wrapping the given Sequel dataset and using url as the template for all generated URLs. url should be a string containing an sprintf flag (such as %s) in place of the page number.



38
39
40
41
# File 'lib/thoth/helper/pagination.rb', line 38

def initialize(dataset, url)
  @dataset = dataset
  @url     = url
end

Instance Method Details

#current_pageObject

Returns the number of the current page.



44
45
46
# File 'lib/thoth/helper/pagination.rb', line 44

def current_page
  @dataset.current_page
end

#current_page_record_countObject

Returns the number of records in the current page.



49
50
51
# File 'lib/thoth/helper/pagination.rb', line 49

def current_page_record_count
  @dataset.current_page_record_count
end

#current_page_record_rangeObject

Returns the record range for the current page.



54
55
56
# File 'lib/thoth/helper/pagination.rb', line 54

def current_page_record_range
  @dataset.current_page_record_range
end

Iterates over all pages within 5 steps from the current page, yielding the page number and URL for each.



60
61
62
63
64
# File 'lib/thoth/helper/pagination.rb', line 60

def navigate # :yields: page, url
  nav_start = [current_page - 5, 1].max
  nav_end   = [nav_start + 9, page_count].min
  (nav_start..nav_end).each {|page| yield page, url(page) }
end

Returns true if the total number of pages is greater than 1.

Returns:

  • (Boolean)


67
68
69
# File 'lib/thoth/helper/pagination.rb', line 67

def navigation?
  page_count > 1
end

#next_pageObject

Returns the number of the next page or nil if the current page is the last.



73
74
75
# File 'lib/thoth/helper/pagination.rb', line 73

def next_page
  @dataset.next_page
end

#next_urlObject

Returns the URL for the next page or nil if the current page is the last.



79
80
81
# File 'lib/thoth/helper/pagination.rb', line 79

def next_url
  next_page ? url(next_page) : nil
end

#page_countObject

Returns the total number of pages.



84
85
86
# File 'lib/thoth/helper/pagination.rb', line 84

def page_count
  @dataset.page_count
end

#page_rangeObject

Returns the page range.



89
90
91
# File 'lib/thoth/helper/pagination.rb', line 89

def page_range
  @dataset.page_range
end

#page_sizeObject

Returns the number of records per page.



94
95
96
# File 'lib/thoth/helper/pagination.rb', line 94

def page_size
  @dataset.page_size
end

#prev_pageObject

Returns the number of the previous page or nil if the current page is the first.



100
101
102
# File 'lib/thoth/helper/pagination.rb', line 100

def prev_page
  @dataset.prev_page
end

#prev_urlObject

Returns the URL for the previous page or nil if the current page is the first.



106
107
108
# File 'lib/thoth/helper/pagination.rb', line 106

def prev_url
  prev_page ? url(prev_page) : nil
end

#record_countObject

Returns the total number of records in the dataset.



111
112
113
# File 'lib/thoth/helper/pagination.rb', line 111

def record_count
  @dataset.pagination_record_count
end

#url(page) ⇒ Object

Returns the URL for the specified page number.



116
117
118
# File 'lib/thoth/helper/pagination.rb', line 116

def url(page)
  @url.to_s.gsub('__page__', page.to_i.to_s)
end