Class: Sunspot::Search::PaginatedCollection

Inherits:
Array
  • 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.



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

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

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



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

def current_page
  @current_page
end

#per_pageObject (readonly) Also known as: limit_value

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#total_countObject Also known as: total_entries

Returns the value of attribute total_count.



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

def total_count
  @total_count
end

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


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

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


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

def last_page?
  current_page >= total_pages
end

#next_pageObject



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

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

#offsetObject Also known as: offset_value



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

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


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

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



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

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

#total_pagesObject Also known as: num_pages



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

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