Class: Jekyll::Pager
- Inherits:
-
Object
- Object
- Jekyll::Pager
- Defined in:
- lib/jekyll/pager.rb
Instance Attribute Summary collapse
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
-
#total_posts ⇒ Object
readonly
Returns the value of attribute total_posts.
Class Method Summary collapse
- .calculate_pages(all_posts, per_page = Jekyll.paginate) ⇒ Object
- .pagination_enabled?(base, file) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(page, all_posts, num_pages = nil, per_page = nil) ⇒ Pager
constructor
A new instance of Pager.
- #to_hash ⇒ Object
Constructor Details
#initialize(page, all_posts, num_pages = nil, per_page = nil) ⇒ Pager
Returns a new instance of Pager.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jekyll/pager.rb', line 17 def initialize(page, all_posts, num_pages = nil, per_page = nil) @page = page @per_page = per_page || Jekyll.paginate @total_pages = num_pages || Pager.calculate_pages(all_posts) if @page > @total_pages raise RuntimeError, "page number can't be grater than total pages: #{@page} > #{@total_pages}" end init = (@page - 1) * @per_page offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1) @total_posts = all_posts.size @posts = all_posts[init..offset] @previous_page = @page != 1 ? @page - 1 : nil @next_page = @page != @total_pages ? @page + 1 : nil end |
Instance Attribute Details
#next_page ⇒ Object (readonly)
Returns the value of attribute next_page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def next_page @next_page end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def per_page @per_page end |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def posts @posts end |
#previous_page ⇒ Object (readonly)
Returns the value of attribute previous_page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def previous_page @previous_page end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def total_pages @total_pages end |
#total_posts ⇒ Object (readonly)
Returns the value of attribute total_posts.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def total_posts @total_posts end |
Class Method Details
Instance Method Details
#to_hash ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll/pager.rb', line 35 def to_hash { 'page' => page, 'per_page' => per_page, 'posts' => posts, 'total_posts' => total_posts, 'total_pages' => total_pages, 'previous_page' => previous_page, 'next_page' => next_page } end |