Class: Page

Inherits:
Object
  • Object
show all
Defined in:
lib/rango/contrib/pagination/page.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Page

Page.new(params, count, per_page) def initialize(current_page, count, per_page)

Raises:

  • (ArgumentError)

Since:

  • 0.0.2



37
38
39
40
41
42
43
44
45
# File 'lib/rango/contrib/pagination/page.rb', line 37

def initialize(params)
  raise ArgumentError unless params.is_a?(Hash)
  @count = params[:count].to_i || raise(ArgumentError, "params[:count] must be given")
  @per_page = (params[:per_page] or 10).to_i
  # can be nil
  @number  = (params[:current].nil? ? 1 : params[:current].to_i)
  @number  = 1   if @number < 1
  @number  = max if @number > max
end

Class Attribute Details

.per_pageObject



10
11
12
# File 'lib/rango/contrib/pagination/page.rb', line 10

def per_page
  @per_page ||= 10
end

.route_hookObject

Since:

  • 0.0.2



6
7
8
# File 'lib/rango/contrib/pagination/page.rb', line 6

def route_hook
  @route_hook
end

Instance Attribute Details

#countObject (readonly)

Since:

  • 0.0.2



32
33
34
# File 'lib/rango/contrib/pagination/page.rb', line 32

def count
  @count
end

#number(type = :human) ⇒ Object

NOTE: offset start as normal array from 0, but page from 1!

Since:

  • 0.0.2



29
30
31
# File 'lib/rango/contrib/pagination/page.rb', line 29

def number
  @number
end

#per_pageObject (readonly)

Since:

  • 0.0.2



32
33
34
# File 'lib/rango/contrib/pagination/page.rb', line 32

def per_page
  @per_page
end

Class Method Details

.register_route_hook(callable = nil, &block) ⇒ Object

register_route_hook do … end OR register_route_hook method(:foo)

Since:

  • 0.0.2



18
19
20
21
# File 'lib/rango/contrib/pagination/page.rb', line 18

def register_route_hook(callable = nil, &block)
  callable = (callable.nil? ? block : callable)
  self.route_hook = callable
end

.route(request, page, int) ⇒ Object



23
24
25
# File 'lib/rango/contrib/pagination/page.rb', line 23

def route(request, page, int)
  self.route_hook.call(request, page, int)
end

Instance Method Details

#firstObject

Since:

  • 0.0.2



90
91
92
# File 'lib/rango/contrib/pagination/page.rb', line 90

def first
  Page.new(current: 1, count: @count, per_page: @per_page)
end

#first?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.2



95
96
97
# File 'lib/rango/contrib/pagination/page.rb', line 95

def first?
  self.number == 1
end

#lastObject

Since:

  • 0.0.2



85
86
87
# File 'lib/rango/contrib/pagination/page.rb', line 85

def last
  Page.new(current: max, count: @count, per_page: @per_page)
end

#last?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.2



100
101
102
# File 'lib/rango/contrib/pagination/page.rb', line 100

def last?
  self.number == max
end

#maxObject

Count of pages

Since:

  • 0.0.2



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

def max
  if @count % @per_page == 0
    @max ||= (@count / @per_page)
  else
    @max ||= (@count / @per_page) + 1
  end
end

#nextObject

Current plus 1 or nil if it’ the last page

Since:

  • 0.0.2



70
71
72
73
74
# File 'lib/rango/contrib/pagination/page.rb', line 70

def next
  if @number < max
    return Page.new(current: @number + 1, count: @count, per_page: @per_page)
  end
end

#previousObject

Current minus 1 or nil if it’ the first page

Since:

  • 0.0.2



78
79
80
81
82
# File 'lib/rango/contrib/pagination/page.rb', line 78

def previous
  if @number > 1
    return Page.new(current: @number - 1, count: @count, per_page: @per_page)
  end
end