Class: Moft::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/moft/generators/pagination.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, page, all_posts, num_pages = nil) ⇒ Pager

Initialize a new Pager.

config - The Hash configuration of the site. page - The Integer page number. all_posts - The Array of all the site’s Posts. num_pages - The Integer number of pages or nil if you’d like the number

of pages calculated.


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/moft/generators/pagination.rb', line 94

def initialize(config, page, all_posts, num_pages = nil)
  @page = page
  @per_page = config['paginate'].to_i
  @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page)

  if @page > @total_pages
    raise RuntimeError, "page number can't be greater 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
  @previous_page_path = Pager.paginate_path(config, @previous_page)
  @next_page = @page != @total_pages ? @page + 1 : nil
  @next_page_path = Pager.paginate_path(config, @next_page)
end

Instance Attribute Details

#next_pageObject (readonly)

Returns the value of attribute next_page.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def next_page
  @next_page
end

#next_page_pathObject (readonly)

Returns the value of attribute next_page_path.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def next_page_path
  @next_page_path
end

#pageObject (readonly)

Returns the value of attribute page.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def previous_page
  @previous_page
end

#previous_page_pathObject (readonly)

Returns the value of attribute previous_page_path.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def previous_page_path
  @previous_page_path
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



52
53
54
# File 'lib/moft/generators/pagination.rb', line 52

def total_posts
  @total_posts
end

Class Method Details

.calculate_pages(all_posts, per_page) ⇒ Object

Calculate the number of pages.

all_posts - The Array of all Posts. per_page - The Integer of entries per page.

Returns the Integer number of pages.



61
62
63
# File 'lib/moft/generators/pagination.rb', line 61

def self.calculate_pages(all_posts, per_page)
  (all_posts.size.to_f / per_page.to_i).ceil
end

.paginate_path(site_config, num_page) ⇒ Object

Static: Return the pagination path of the page

site_config - the site config num_page - the pagination page number

Returns the pagination path as a string



81
82
83
84
85
# File 'lib/moft/generators/pagination.rb', line 81

def self.paginate_path(site_config, num_page)
  return nil if num_page.nil? || num_page <= 1
  format = site_config['paginate_path']
  format.sub(':num', num_page.to_s)
end

.pagination_enabled?(config, file) ⇒ Boolean

Determine if pagination is enabled for a given file.

config - The configuration Hash. file - The String filename of the file.

Returns true if pagination is enabled, false otherwise.

Returns:

  • (Boolean)


71
72
73
# File 'lib/moft/generators/pagination.rb', line 71

def self.pagination_enabled?(config, file)
  file == 'index.html' && !config['paginate'].nil?
end

Instance Method Details

#to_liquidObject

Convert this Pager’s data to a Hash suitable for use by Liquid.

Returns the Hash representation of this Pager.



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/moft/generators/pagination.rb', line 117

def to_liquid
  {
    'page' => page,
    'per_page' => per_page,
    'posts' => posts,
    'total_posts' => total_posts,
    'total_pages' => total_pages,
    'previous_page' => previous_page,
    'previous_page_path' => previous_page_path,
    'next_page' => next_page,
    'next_page_path' => next_page_path
  }
end