Class: RailsSimpleEventSourcing::Paginator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, page:, per_page:) ⇒ Paginator

Returns a new instance of Paginator.



7
8
9
10
11
12
13
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 7

def initialize(scope:, page:, per_page:)
  @scope = scope
  @per_page = per_page
  @total_count = scope.count
  @total_pages = [(@total_count.to_f / per_page).ceil, 1].max
  @current_page = (page.presence || 1).to_i.clamp(1, @total_pages)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



5
6
7
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



5
6
7
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5

def per_page
  @per_page
end

#total_countObject (readonly)

Returns the value of attribute total_count.



5
6
7
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5

def total_count
  @total_count
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



5
6
7
# File 'lib/rails_simple_event_sourcing/paginator.rb', line 5

def total_pages
  @total_pages
end

Instance Method Details

#recordsObject



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

def records
  @scope.offset((@current_page - 1) * @per_page).limit(@per_page)
end