Class: Paginator::Page

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

Overview

Page object

Retrieves items for a page and provides metadata about the position of the page in the paginator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pager, number, select) ⇒ Page

:nodoc:



74
75
76
77
78
# File 'lib/paginator.rb', line 74

def initialize(pager, number, select) #:nodoc:
  @pager, @number = pager, number
  @offset = (number - 1) * pager.per_page
  @select = select
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

:nodoc:



128
129
130
131
132
133
134
# File 'lib/paginator.rb', line 128

def method_missing(meth, *args, &block) #:nodoc:
  if @pager.respond_to?(meth)
    @pager.__send__(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#pagerObject (readonly)

Returns the value of attribute pager.



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

def pager
  @pager
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



120
121
122
# File 'lib/paginator.rb', line 120

def ==(other) #:nodoc:
  @pager == other.pager && self.number == other.number
end

#each(&block) ⇒ Object



124
125
126
# File 'lib/paginator.rb', line 124

def each(&block)
  items.each(&block)
end

#first_item_numberObject

The “item number” of the first item on this page



107
108
109
# File 'lib/paginator.rb', line 107

def first_item_number
  1 + @offset
end

#itemsObject

Retrieve the items for this page

  • Caches



82
83
84
# File 'lib/paginator.rb', line 82

def items
  @items ||= @select.call
end

#last_item_numberObject

The “item number” of the last item on this page



112
113
114
115
116
117
118
# File 'lib/paginator.rb', line 112

def last_item_number
  if next?
    @offset + @pager.per_page
  else
    @pager.count
  end
end

#nextObject

Get next page (if possible)



102
103
104
# File 'lib/paginator.rb', line 102

def next
  @pager.page(@number + 1) if next?
end

#next?Boolean

Checks to see if there’s a page after this one

Returns:

  • (Boolean)


97
98
99
# File 'lib/paginator.rb', line 97

def next?
  @number < @pager.number_of_pages
end

#prevObject

# Get previous page (if possible)



92
93
94
# File 'lib/paginator.rb', line 92

def prev
  @pager.page(@number - 1) if prev?
end

#prev?Boolean

Checks to see if there’s a page before this one

Returns:

  • (Boolean)


87
88
89
# File 'lib/paginator.rb', line 87

def prev?
  @number > 1
end