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

Direct Known Subclasses

DataMapperPager

Instance Method Summary collapse

Constructor Details

#initialize(array, page, limit) ⇒ ArrayPager

Returns a new instance of ArrayPager.



241
242
243
244
# File 'lib/ramaze/helper/paginate.rb', line 241

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

Instance Method Details

#current_pageObject



259
260
261
# File 'lib/ramaze/helper/paginate.rb', line 259

def current_page
  @page
end

#each(&block) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/ramaze/helper/paginate.rb', line 279

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

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

#empty?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/ramaze/helper/paginate.rb', line 250

def empty?
  @array.empty?
end

#first_page?Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/ramaze/helper/paginate.rb', line 271

def first_page?
  @page <= 1
end

#last_page?Boolean

Returns:

  • (Boolean)


275
276
277
# File 'lib/ramaze/helper/paginate.rb', line 275

def last_page?
  page_count == @page
end

#next_pageObject



263
264
265
# File 'lib/ramaze/helper/paginate.rb', line 263

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

#page_countObject



254
255
256
257
# File 'lib/ramaze/helper/paginate.rb', line 254

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

#prev_pageObject



267
268
269
# File 'lib/ramaze/helper/paginate.rb', line 267

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

#sizeObject



246
247
248
# File 'lib/ramaze/helper/paginate.rb', line 246

def size
  @array.size
end