Class: Kiva::PagedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/ruby-kiva/paged_array.rb

Overview

Adds pagination information to array Pagination attributes/methods have the same names as the wonderful will_paginate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ PagedArray

Returns a new instance of PagedArray.



7
8
9
10
11
12
13
# File 'lib/ruby-kiva/paged_array.rb', line 7

def initialize(data, options = {})
  super data
  @current_page = options['page'] || 1
  @per_page = options['page_size'] || data.size
  @total_pages = options['pages'] || 1
  @total_entries = options['total'] || data.size
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



5
6
7
# File 'lib/ruby-kiva/paged_array.rb', line 5

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



5
6
7
# File 'lib/ruby-kiva/paged_array.rb', line 5

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



5
6
7
# File 'lib/ruby-kiva/paged_array.rb', line 5

def total_entries
  @total_entries
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



5
6
7
# File 'lib/ruby-kiva/paged_array.rb', line 5

def total_pages
  @total_pages
end

Instance Method Details

#from_entryObject



35
36
37
# File 'lib/ruby-kiva/paged_array.rb', line 35

def from_entry
  self.offset + 1 unless self.out_of_bounds?
end

#inspectObject



47
48
49
# File 'lib/ruby-kiva/paged_array.rb', line 47

def inspect
  "#{pagination_info}\n#{super}"
end

#next_pageObject



23
24
25
# File 'lib/ruby-kiva/paged_array.rb', line 23

def next_page
  self.current_page + 1 if next_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ruby-kiva/paged_array.rb', line 19

def next_page?()
  self.current_page < self.total_pages
end

#offsetObject



15
16
17
# File 'lib/ruby-kiva/paged_array.rb', line 15

def offset
  (self.current_page - 1) * self.per_page
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ruby-kiva/paged_array.rb', line 39

def out_of_bounds?
  self.current_page < 1 || self.current_page > self.total_pages
end

#previous_pageObject



27
28
29
# File 'lib/ruby-kiva/paged_array.rb', line 27

def previous_page
  self.current_page - 1 if previous_page?
end

#previous_page?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/ruby-kiva/paged_array.rb', line 31

def previous_page?()
  self.current_page > 1
end

#to_entryObject



43
44
45
# File 'lib/ruby-kiva/paged_array.rb', line 43

def to_entry
  [self.offset + self.per_page, self.total_entries].min unless self.out_of_bounds?
end

#to_sObject



51
52
53
# File 'lib/ruby-kiva/paged_array.rb', line 51

def to_s
  self.inspect
end