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.



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

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)



49
50
51
# File 'lib/sunspot/search/paginated_collection.rb', line 49

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 (readonly) 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)


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

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


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

def last_page?
  current_page >= total_pages
end

#next_pageObject



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

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

#offsetObject



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

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


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

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



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

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

#total_pagesObject Also known as: num_pages



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

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