Class: Dimples::Pager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pager.



13
14
15
16
17
18
19
20
21
# File 'lib/dimples/pager.rb', line 13

def initialize(url, posts, options = {})
  @url = url
  @posts = posts
  @per_page = options[:per_page] || 10
  @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.



7
8
9
# File 'lib/dimples/pager.rb', line 7

def current_page
  @current_page
end

#item_countObject (readonly)

Returns the value of attribute item_count.



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

def item_count
  @item_count
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



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

def next_page
  @next_page
end

#page_countObject (readonly)

Returns the value of attribute page_count.



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

def page_count
  @page_count
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



8
9
10
# File 'lib/dimples/pager.rb', line 8

def previous_page
  @previous_page
end

Instance Method Details

#current_page_urlObject



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

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

#each(&block) ⇒ Object



23
24
25
26
27
# File 'lib/dimples/pager.rb', line 23

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

#first_page_urlObject



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

def first_page_url
  @url
end

#last_page_urlObject



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

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

#next_page_urlObject



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

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

#posts_at(page) ⇒ Object



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

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

#previous_page_urlObject



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

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

#step_to(page) ⇒ Object



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

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



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

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