Class: RailsPulse::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_pulse/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count:, page:, limit:) ⇒ Paginator

Returns a new instance of Paginator.



5
6
7
8
9
# File 'lib/rails_pulse/paginator.rb', line 5

def initialize(count:, page:, limit:)
  @count = count
  @limit = limit
  @page  = page.clamp(1, last)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/rails_pulse/paginator.rb', line 3

def count
  @count
end

#limitObject (readonly)

Returns the value of attribute limit.



3
4
5
# File 'lib/rails_pulse/paginator.rb', line 3

def limit
  @limit
end

#pageObject (readonly)

Returns the value of attribute page.



3
4
5
# File 'lib/rails_pulse/paginator.rb', line 3

def page
  @page
end

Instance Method Details

#lastObject



11
12
13
# File 'lib/rails_pulse/paginator.rb', line 11

def last
  [ (count.to_f / limit).ceil, 1 ].max
end

#nextObject



19
20
21
# File 'lib/rails_pulse/paginator.rb', line 19

def next
  page < last ? page + 1 : nil
end

#previousObject



15
16
17
# File 'lib/rails_pulse/paginator.rb', line 15

def previous
  page > 1 ? page - 1 : nil
end