Class: Dimples::Pager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dimples/pager.rb

Overview

A paginated collection of posts that can be walked forward or backwards.

Constant Summary collapse

PER_PAGE_DEFAULT =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, posts, options = {}) ⇒ Pager

Returns a new instance of Pager.



40
41
42
43
44
45
46
47
48
# File 'lib/dimples/pager.rb', line 40

def initialize(url, posts, options = {})
  @url = url
  @posts = posts
  @per_page = options[:per_page] || PER_PAGE_DEFAULT
  @page_prefix = options[:page_prefix] || 'page_'
  @page_count = (posts.length.to_f / @per_page.to_i).ceil

  step_to(1)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



10
11
12
# File 'lib/dimples/pager.rb', line 10

def current_page
  @current_page
end

#item_countObject (readonly)

Returns the value of attribute item_count.



14
15
16
# File 'lib/dimples/pager.rb', line 14

def item_count
  @item_count
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



12
13
14
# File 'lib/dimples/pager.rb', line 12

def next_page
  @next_page
end

#page_countObject (readonly)

Returns the value of attribute page_count.



13
14
15
# File 'lib/dimples/pager.rb', line 13

def page_count
  @page_count
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



11
12
13
# File 'lib/dimples/pager.rb', line 11

def previous_page
  @previous_page
end

Class Method Details

.paginate(site, posts, path, layout, context = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dimples/pager.rb', line 16

def self.paginate(site, posts, path, layout, context = {})
  pager = Pager.new(
    path.sub(site.paths[:destination], '') + '/',
    posts,
    site.config.pagination
  )

  pager.each do |index|
    page = Page.new(site)
    page.layout = layout

    page_path = if index == 1
                  path
                else
                  File.join(path, "page_#{index}")
                end

    page.write(
      page_path,
      context.merge(pagination: pager.to_context)
    )
  end
end

Instance Method Details

#current_page_urlObject



74
75
76
# File 'lib/dimples/pager.rb', line 74

def current_page_url
  @current_page != 1 ? "#{@url}#{@page_prefix}#{@current_page}" : @url
end

#each(&block) ⇒ Object



50
51
52
# File 'lib/dimples/pager.rb', line 50

def each(&block)
  (1..@page_count).each { |index| block.yield step_to(index) }
end

#first_page_urlObject



78
79
80
# File 'lib/dimples/pager.rb', line 78

def first_page_url
  @url
end

#last_page_urlObject



82
83
84
# File 'lib/dimples/pager.rb', line 82

def last_page_url
  @page_count != 1 ? "#{@url}#{@page_prefix}#{@page_count}" : @url
end

#next_page?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/dimples/pager.rb', line 70

def next_page?
  @current_page + 1 <= @page_count
end

#next_page_urlObject



92
93
94
# File 'lib/dimples/pager.rb', line 92

def next_page_url
  "#{@url}#{@page_prefix}#{@next_page}" if @next_page
end

#posts_at(page) ⇒ Object



62
63
64
# File 'lib/dimples/pager.rb', line 62

def posts_at(page)
  @posts.slice((page - 1) * @per_page, @per_page)
end

#previous_page?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dimples/pager.rb', line 66

def previous_page?
  (@current_page - 1).positive?
end

#previous_page_urlObject



86
87
88
89
90
# File 'lib/dimples/pager.rb', line 86

def previous_page_url
  return unless @previous_page

  @previous_page != 1 ? "#{@url}#{@page_prefix}#{@previous_page}" : @url
end

#step_to(page) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/dimples/pager.rb', line 54

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

#to_contextObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dimples/pager.rb', line 96

def to_context
  Hashie::Mash.new(
    posts: posts_at(current_page),
    current_page: @current_page,
    page_count: @page_count,
    post_count: @posts.count,
    previous_page: @previous_page,
    next_page: @next_page,
    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