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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pager.



16
17
18
19
20
21
22
23
24
# File 'lib/dimples/pager.rb', line 16

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

Instance Method Details

#current_page_urlObject



42
43
44
# File 'lib/dimples/pager.rb', line 42

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

#each(&block) ⇒ Object



26
27
28
# File 'lib/dimples/pager.rb', line 26

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

#first_page_urlObject



46
47
48
# File 'lib/dimples/pager.rb', line 46

def first_page_url
  @url
end

#last_page_urlObject



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

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

#next_page_urlObject



59
60
61
# File 'lib/dimples/pager.rb', line 59

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

#posts_at(page) ⇒ Object



38
39
40
# File 'lib/dimples/pager.rb', line 38

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

#previous_page_urlObject



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

def previous_page_url
  return unless @previous_page
  @previous_page != 1 ? "#{@url}#{@page_prefix}#{@previous_page}" : @url
end

#step_to(page) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/dimples/pager.rb', line 30

def step_to(page)
  @current_page = (1..@page_count).cover?(page) ? page : 1
  @previous_page = (@current_page - 1).positive? ? @current_page - 1 : nil
  @next_page = @current_page + 1 <= @page_count ? @current_page + 1 : nil

  @current_page
end

#to_contextObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dimples/pager.rb', line 63

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