Class: Paginate::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/paginate/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
# File 'lib/paginate/base.rb', line 5

def initialize(options = {})
  if options.kind_of?(Hash)
    @options = options
  else
    @options = {:page => options.to_i}
  end

  @options.reverse_merge!(Paginate::Config.to_hash)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/paginate/base.rb', line 3

def options
  @options
end

Instance Method Details

#collection_sizeObject



15
16
17
# File 'lib/paginate/base.rb', line 15

def collection_size
  @collection_size ||= options[:collection].size
end

#limitObject



35
36
37
# File 'lib/paginate/base.rb', line 35

def limit
  [options[:size], 10].compact.first.to_i + 1
end

#next_page?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/paginate/base.rb', line 19

def next_page?
  collection_size > options[:size]
end

#offsetObject



31
32
33
# File 'lib/paginate/base.rb', line 31

def offset
  (page - 1) * (limit - 1)
end

#pageObject



27
28
29
# File 'lib/paginate/base.rb', line 27

def page
  [1, options.fetch(:page, 1).to_i].max
end

#previous_page?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/paginate/base.rb', line 23

def previous_page?
  options[:page] > 1
end

#to_optionsObject



39
40
41
# File 'lib/paginate/base.rb', line 39

def to_options
  { :limit => limit, :offset => offset }
end