Class: DmPagination::Paginator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-pagination/paginator.rb

Direct Known Subclasses

Solo, Trio

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dm-pagination/paginator.rb', line 6

def initialize(collection, options)
  @proxy_collection = collection
  order = options[:order]

  @page = (options[:page] || 1).to_i
  @per_page = (options[:per_page] || 10).to_i
  @num_pages = (@proxy_collection.count + @per_page - 1) / @per_page
  @offset = (@page - 1)*@per_page

  @collection = collection.all(:offset => @offset, :limit => @per_page)
  @collection = @collection.all(:order => order) if order
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



27
28
29
# File 'lib/dm-pagination/paginator.rb', line 27

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

Instance Attribute Details

#num_pagesObject (readonly)

Returns the value of attribute num_pages.



4
5
6
# File 'lib/dm-pagination/paginator.rb', line 4

def num_pages
  @num_pages
end

#pageObject (readonly)

Returns the value of attribute page.



4
5
6
# File 'lib/dm-pagination/paginator.rb', line 4

def page
  @page
end

Instance Method Details

#countObject



19
20
21
# File 'lib/dm-pagination/paginator.rb', line 19

def count
  [0, [@proxy_collection.count - @offset, @per_page].min].max
end

#respond_to?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dm-pagination/paginator.rb', line 23

def respond_to?(*args, &block)
  super || @collection.send(:respond_to?, *args, &block)
end

#to_jsonObject



31
32
33
# File 'lib/dm-pagination/paginator.rb', line 31

def to_json
  @collection.to_json
end