Class: ApiOnlyPagination::Paginate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, per_page, count) ⇒ Paginate

Returns a new instance of Paginate.



7
8
9
10
11
12
13
14
# File 'lib/api_only_pagination/paginate.rb', line 7

def initialize(page, per_page, count)
  @page     = (page || ApiOnlyPagination.configuration.default_page).to_i
  @count    = count
  @per_page = [(per_page.to_i || ApiOnlyPagination.configuration.default_page_size),
               ApiOnlyPagination.configuration.max_per_page].min

  validate_params
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



5
6
7
# File 'lib/api_only_pagination/paginate.rb', line 5

def count
  @count
end

#pageObject (readonly)

Returns the value of attribute page.



5
6
7
# File 'lib/api_only_pagination/paginate.rb', line 5

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



5
6
7
# File 'lib/api_only_pagination/paginate.rb', line 5

def per_page
  @per_page
end

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/api_only_pagination/paginate.rb', line 47

def first_page?
  page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/api_only_pagination/paginate.rb', line 43

def last_page?
  page == total_pages
end

#next_pageObject



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

def next_page
  page + 1 unless last_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


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

def next_page?
  page < total_pages
end

#offsetObject



21
22
23
24
25
# File 'lib/api_only_pagination/paginate.rb', line 21

def offset
  return 0 if page == 1

  per_page * (page.to_i - 1)
end

#previous_pageObject



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

def previous_page
  page - 1 unless first_page?
end

#previous_page?Boolean

Returns:

  • (Boolean)


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

def previous_page?
  page > 1
end

#total_pagesObject



51
52
53
# File 'lib/api_only_pagination/paginate.rb', line 51

def total_pages
  (count / per_page.to_f).ceil
end

#validate_paramsObject



16
17
18
19
# File 'lib/api_only_pagination/paginate.rb', line 16

def validate_params
  @page = @page.zero? ? ApiOnlyPagination.configuration.default_page : @page
  @per_page = @per_page.zero? ? ApiOnlyPagination.configuration.default_page_size : @per_page
end