Class: Ramaze::Helper::Paginate::Paginator::ArrayPager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ramaze/helper/paginate.rb

Overview

Wrapper for Array to behave like the Sequel pagination

Instance Method Summary collapse

Constructor Details

#initialize(array, page, limit) ⇒ ArrayPager

Returns a new instance of ArrayPager.



181
182
183
184
# File 'lib/ramaze/helper/paginate.rb', line 181

def initialize(array, page, limit)
  @array, @page, @limit = array, page, limit
  @page = page_count if @page > page_count
end

Instance Method Details

#current_pageObject



199
200
201
# File 'lib/ramaze/helper/paginate.rb', line 199

def current_page
  @page
end

#each(&block) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/ramaze/helper/paginate.rb', line 219

def each(&block)
  from = ((@page - 1) * @limit)
  to = from + @limit

  a = @array[from...to] || []
  a.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/ramaze/helper/paginate.rb', line 190

def empty?
  @array.empty?
end

#first_page?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/ramaze/helper/paginate.rb', line 211

def first_page?
  @page <= 1
end

#last_page?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/ramaze/helper/paginate.rb', line 215

def last_page?
  page_count == @page
end

#next_pageObject



203
204
205
# File 'lib/ramaze/helper/paginate.rb', line 203

def next_page
  page_count == @page ? nil : @page + 1
end

#page_countObject



194
195
196
197
# File 'lib/ramaze/helper/paginate.rb', line 194

def page_count
  pages, rest = @array.size.divmod(@limit)
  rest == 0 ? pages : pages + 1
end

#prev_pageObject



207
208
209
# File 'lib/ramaze/helper/paginate.rb', line 207

def prev_page
  @page <= 1 ? nil : @page - 1
end

#sizeObject



186
187
188
# File 'lib/ramaze/helper/paginate.rb', line 186

def size
  @array.size
end