Class: Dimples::Pagination::Pager

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

Overview

A class that models the context of a single page during pagination.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pager.



44
45
46
47
48
49
50
51
52
# File 'lib/dimples/pagination.rb', line 44

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

  step_to(1)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



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

def current_page
  @current_page
end

#item_countObject (readonly)

Returns the value of attribute item_count.



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

def item_count
  @item_count
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



40
41
42
# File 'lib/dimples/pagination.rb', line 40

def next_page
  @next_page
end

#page_countObject (readonly)

Returns the value of attribute page_count.



41
42
43
# File 'lib/dimples/pagination.rb', line 41

def page_count
  @page_count
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



39
40
41
# File 'lib/dimples/pagination.rb', line 39

def previous_page
  @previous_page
end

Instance Method Details

#current_page_urlObject



72
73
74
# File 'lib/dimples/pagination.rb', line 72

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

#each(&block) ⇒ Object



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

def each(&block)
  (1..@page_count).each do |index|
    block.yield step_to(index), items_at(index)
  end
end

#first_page_urlObject



76
77
78
# File 'lib/dimples/pagination.rb', line 76

def first_page_url
  @url
end

#items_at(page) ⇒ Object



68
69
70
# File 'lib/dimples/pagination.rb', line 68

def items_at(page)
  @items.slice((page - 1) * @per_page, @per_page)
end

#last_page_urlObject



80
81
82
# File 'lib/dimples/pagination.rb', line 80

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

#next_page_urlObject



89
90
91
# File 'lib/dimples/pagination.rb', line 89

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

#previous_page_urlObject



84
85
86
87
# File 'lib/dimples/pagination.rb', line 84

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

#step_to(page) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dimples/pagination.rb', line 60

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_hObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dimples/pagination.rb', line 93

def to_h
  {
    url: @url,
    current_page: @current_page,
    page_count: @page_count,
    item_count: @items.count,
    previous_page: @previous_page,
    next_page: @next_page,
    links: {
      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