Class: Supplejack::PaginatedCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/supplejack/paginated_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, page, per_page, total) ⇒ PaginatedCollection

Returns a new instance of PaginatedCollection.



20
21
22
23
24
25
# File 'lib/supplejack/paginated_collection.rb', line 20

def initialize(collection, page, per_page, total)
  @collection   = collection
  @current_page = page
  @per_page     = per_page
  @total_count  = total
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



58
59
60
# File 'lib/supplejack/paginated_collection.rb', line 58

def method_missing(method, *args, &block)
  @collection.send(method, *args, &block)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



14
15
16
# File 'lib/supplejack/paginated_collection.rb', line 14

def current_page
  @current_page
end

#per_pageObject (readonly) Also known as: limit_value

Returns the value of attribute per_page.



14
15
16
# File 'lib/supplejack/paginated_collection.rb', line 14

def per_page
  @per_page
end

#total_countObject Also known as: total_entries

Returns the value of attribute total_count.



15
16
17
# File 'lib/supplejack/paginated_collection.rb', line 15

def total_count
  @total_count
end

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/supplejack/paginated_collection.rb', line 32

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/supplejack/paginated_collection.rb', line 36

def last_page?
  current_page >= total_pages
end

#next_pageObject



44
45
46
# File 'lib/supplejack/paginated_collection.rb', line 44

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

#offsetObject



52
53
54
# File 'lib/supplejack/paginated_collection.rb', line 52

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/supplejack/paginated_collection.rb', line 48

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



40
41
42
# File 'lib/supplejack/paginated_collection.rb', line 40

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

#total_pagesObject Also known as: num_pages



27
28
29
# File 'lib/supplejack/paginated_collection.rb', line 27

def total_pages
  (total_count.to_f / per_page).ceil
end