Class: Dimples::Pager
- Inherits:
-
Object
- Object
- Dimples::Pager
- Includes:
- Enumerable
- Defined in:
- lib/dimples/pager.rb
Overview
A class for paginating a collection of posts.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#page_count ⇒ Object
readonly
Returns the value of attribute page_count.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
Class Method Summary collapse
Instance Method Summary collapse
- #current_page_url ⇒ Object
- #first_page_url ⇒ Object
-
#initialize(site:, url:, posts:) ⇒ Pager
constructor
A new instance of Pager.
- #last_page_url ⇒ Object
- #metadata ⇒ Object
- #next_page? ⇒ Boolean
- #next_page_url ⇒ Object
- #paginate(context: {}) ⇒ Object
- #posts_at(page) ⇒ Object
- #previous_page? ⇒ Boolean
- #previous_page_url ⇒ Object
- #step_to(page) ⇒ Object
- #urls ⇒ Object
Constructor Details
#initialize(site:, url:, posts:) ⇒ Pager
Returns a new instance of Pager.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dimples/pager.rb', line 14 def initialize(site:, url:, posts:) @site = site @url = url @posts = posts @per_page = @site.config.pagination[:per_page] @page_prefix = @site.config.pagination[:page_prefix] @page_count = (posts.length.to_f / @per_page.to_i).ceil step_to(1) end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
8 9 10 |
# File 'lib/dimples/pager.rb', line 8 def current_page @current_page end |
#next_page ⇒ Object (readonly)
Returns the value of attribute next_page.
8 9 10 |
# File 'lib/dimples/pager.rb', line 8 def next_page @next_page end |
#page_count ⇒ Object (readonly)
Returns the value of attribute page_count.
8 9 10 |
# File 'lib/dimples/pager.rb', line 8 def page_count @page_count end |
#previous_page ⇒ Object (readonly)
Returns the value of attribute previous_page.
8 9 10 |
# File 'lib/dimples/pager.rb', line 8 def previous_page @previous_page end |
Class Method Details
.paginate(site:, url:, posts:, context: {}, &block) ⇒ Object
10 11 12 |
# File 'lib/dimples/pager.rb', line 10 def self.paginate(site:, url:, posts:, context: {}, &block) new(site: site, url: url, posts: posts).paginate(context: context, &block) end |
Instance Method Details
#current_page_url ⇒ Object
63 64 65 |
# File 'lib/dimples/pager.rb', line 63 def current_page_url @current_page == 1 ? @url : "#{@url}#{@page_prefix}#{@current_page}" end |
#first_page_url ⇒ Object
67 68 69 |
# File 'lib/dimples/pager.rb', line 67 def first_page_url @url end |
#last_page_url ⇒ Object
71 72 73 |
# File 'lib/dimples/pager.rb', line 71 def last_page_url @page_count == 1 ? @url : "#{@url}#{@page_prefix}#{@page_count}" end |
#metadata ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dimples/pager.rb', line 95 def { posts: posts_at(current_page), pagination: { current_page: @current_page, page_count: @page_count, post_count: @posts.count, previous_page: @previous_page, next_page: @next_page, urls: urls } } end |
#next_page? ⇒ Boolean
59 60 61 |
# File 'lib/dimples/pager.rb', line 59 def next_page? @current_page + 1 <= @page_count end |
#next_page_url ⇒ Object
81 82 83 |
# File 'lib/dimples/pager.rb', line 81 def next_page_url "#{@url}#{@page_prefix}#{@next_page}" if @next_page end |
#paginate(context: {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dimples/pager.rb', line 26 def paginate(context: {}) (1..@page_count).each do |index| step_to(index) output_directory = File.join(@site.config.build_paths[:root], current_page_url) context.merge!(, url: current_page_url) @site.layouts[:posts]&.generate( output_path: File.join(output_directory, 'index.html'), context: context ) yield(output_directory, context) if block_given? end end |
#posts_at(page) ⇒ Object
51 52 53 |
# File 'lib/dimples/pager.rb', line 51 def posts_at(page) @posts.slice((page - 1) * @per_page, @per_page) end |
#previous_page? ⇒ Boolean
55 56 57 |
# File 'lib/dimples/pager.rb', line 55 def previous_page? (@current_page - 1).positive? end |
#previous_page_url ⇒ Object
75 76 77 78 79 |
# File 'lib/dimples/pager.rb', line 75 def previous_page_url return unless @previous_page @previous_page == 1 ? @url : "#{@url}#{@page_prefix}#{@previous_page}" end |
#step_to(page) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/dimples/pager.rb', line 43 def step_to(page) @current_page = page @previous_page = previous_page? ? @current_page - 1 : nil @next_page = next_page? ? @current_page + 1 : nil @current_page end |
#urls ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/dimples/pager.rb', line 85 def urls { current_page: current_page_url, first_page: first_page_url, last_page: last_page_url, previous_page: previous_page_url, next_page: next_page_url } end |