Class: ApiMe::Pagination

Inherits:
Object
  • Object
show all
Defined in:
lib/api_me/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:, page_params:) ⇒ Pagination

Returns a new instance of Pagination.



5
6
7
8
9
10
11
12
# File 'lib/api_me/pagination.rb', line 5

def initialize(scope:, page_params:)
  self.scope = scope

  if page_params
    self.page_size = page_params[:size]
    self.page_offset = page_params[:offset]
  end
end

Instance Attribute Details

#page_offsetObject

Returns the value of attribute page_offset.



3
4
5
# File 'lib/api_me/pagination.rb', line 3

def page_offset
  @page_offset
end

#page_sizeObject

Returns the value of attribute page_size.



3
4
5
# File 'lib/api_me/pagination.rb', line 3

def page_size
  @page_size
end

#scopeObject

Returns the value of attribute scope.



3
4
5
# File 'lib/api_me/pagination.rb', line 3

def scope
  @scope
end

Instance Method Details

#page_metaObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/api_me/pagination.rb', line 18

def page_meta
  return Hash.new unless paging?
  {
    size: page_size.nil? ? default_page_size : page_size,
    offset: page_offset,
    record_count: scope.size,
    total_records: scope.total_count,
    total_pages: scope.total_pages
  }
end

#resultsObject



14
15
16
# File 'lib/api_me/pagination.rb', line 14

def results
  paging? ? page.per.scope : scope
end