Class: Sunspot::Search::PaginatedCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/search/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.



13
14
15
16
17
18
# File 'lib/sunspot/search/paginated_collection.rb', line 13

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)



51
52
53
# File 'lib/sunspot/search/paginated_collection.rb', line 51

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.



7
8
9
# File 'lib/sunspot/search/paginated_collection.rb', line 7

def current_page
  @current_page
end

#per_pageObject (readonly) Also known as: limit_value

Returns the value of attribute per_page.



7
8
9
# File 'lib/sunspot/search/paginated_collection.rb', line 7

def per_page
  @per_page
end

#total_countObject Also known as: total_entries

Returns the value of attribute total_count.



8
9
10
# File 'lib/sunspot/search/paginated_collection.rb', line 8

def total_count
  @total_count
end

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/sunspot/search/paginated_collection.rb', line 25

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/sunspot/search/paginated_collection.rb', line 29

def last_page?
  current_page >= total_pages
end

#next_pageObject



37
38
39
# File 'lib/sunspot/search/paginated_collection.rb', line 37

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

#offsetObject



45
46
47
# File 'lib/sunspot/search/paginated_collection.rb', line 45

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/sunspot/search/paginated_collection.rb', line 41

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



33
34
35
# File 'lib/sunspot/search/paginated_collection.rb', line 33

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

#total_pagesObject Also known as: num_pages



20
21
22
# File 'lib/sunspot/search/paginated_collection.rb', line 20

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