Class: Jekyll::CollectionPages::TagPager

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/collection_pages/tag_pager.rb

Constant Summary collapse

LIQUID_MAP =
{
  'page' => :page, # the current page number
  'per_page' => :per_page, # the number of posts per page
  'posts' => :posts, # the paginated posts for this page
  'total_posts' => :total_posts, # the total number of posts being paginated
  'total_pages' => :total_pages, # the total number of pages
  'previous_page' => :previous_page, # the previous page number, or nil
  'next_page' => :next_page # the next page number, or nil
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_num, per_page, all_posts) ⇒ TagPager

Returns a new instance of TagPager.



26
27
28
29
30
31
32
33
34
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 26

def initialize(page_num, per_page, all_posts)
  @page = page_num
  @per_page = per_page.to_i.positive? ? per_page.to_i : 0
  @total_posts = all_posts.size
  @total_pages = self.class.calculate_pages(all_posts, @per_page)
  @posts = slice_posts(all_posts)
  @previous_page = previous_page_number
  @next_page = next_page_number(total_pages)
end

Instance Attribute Details

#next_pageObject (readonly)

Returns the value of attribute next_page.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def next_page
  @next_page
end

#pageObject (readonly)

Returns the value of attribute page.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def previous_page
  @previous_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



6
7
8
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 6

def total_posts
  @total_posts
end

Class Method Details

.calculate_pages(all_posts, per_page) ⇒ Object



19
20
21
22
23
24
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 19

def self.calculate_pages(all_posts, per_page)
  per_page_value = per_page.to_i
  return 1 if per_page_value <= 0

  (all_posts.size.to_f / per_page_value).ceil
end

Instance Method Details

#to_liquidObject



36
37
38
# File 'lib/jekyll/collection_pages/tag_pager.rb', line 36

def to_liquid
  LIQUID_MAP.transform_values { |reader| public_send(reader) }
end