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) ⇒ Pager



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

def initialize(url, items, per_page)
  @url = url
  @items = items
  @per_page = per_page
  @page_count = (items.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.



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

def current_page
  @current_page
end

#item_countObject (readonly)

Returns the value of attribute item_count.



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

def item_count
  @item_count
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



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

def next_page
  @next_page
end

#page_countObject (readonly)

Returns the value of attribute page_count.



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

def page_count
  @page_count
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



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

def previous_page
  @previous_page
end

Instance Method Details

#each(&block) ⇒ Object



52
53
54
55
56
# File 'lib/dimples/pagination.rb', line 52

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

#items_at(page) ⇒ Object



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

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

#next_page_urlObject



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

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

#previous_page_urlObject



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

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

#step_to(page) ⇒ Object



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

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dimples/pagination.rb', line 79

def to_h
  output = {
    current_page: @current_page,
    page_count: @page_count,
    item_count: @items.count,
    url: @url
  }

  if @previous_page
    output[:previous_page] = @previous_page
    output[:previous_page_url] = previous_page_url
  end

  if @next_page
    output[:next_page] = @next_page
    output[:next_page_url] = next_page_url
  end

  output
end