Class: ChargeIO::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/chargeio/models/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, per_page, total) ⇒ Collection

Returns a new instance of Collection.


6
7
8
9
10
# File 'lib/chargeio/models/collection.rb', line 6

def initialize(page, per_page, total)
  @current_page = page.to_i
  @per_page = per_page.to_i
  @total_entries = total.to_i
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page


4
5
6
# File 'lib/chargeio/models/collection.rb', line 4

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page


4
5
6
# File 'lib/chargeio/models/collection.rb', line 4

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries


4
5
6
# File 'lib/chargeio/models/collection.rb', line 4

def total_entries
  @total_entries
end

Instance Method Details

#next_pageObject

current_page + 1 or nil if there is no next page


22
23
24
# File 'lib/chargeio/models/collection.rb', line 22

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#out_of_bounds?Boolean

Helper method that is true when someone tries to fetch a page with a larger number than the last page. Can be used in combination with flashes and redirecting.

Returns:

  • (Boolean)

29
30
31
# File 'lib/chargeio/models/collection.rb', line 29

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject

current_page - 1 or nil if there is no previous page


17
18
19
# File 'lib/chargeio/models/collection.rb', line 17

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#total_pagesObject


12
13
14
# File 'lib/chargeio/models/collection.rb', line 12

def total_pages
  total_entries.zero? ? 1 : (total_entries / per_page.to_f).ceil
end