Class: Abqari::Paginator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(posts:, page:, total_pages:, base_url:) ⇒ Paginator

Returns a new instance of Paginator.



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

def initialize(posts:, page:, total_pages:, base_url:)
  @posts = posts
  @page = page
  @total_pages = total_pages
  @base_url = base_url.sub(%r{/?\z}, '/')
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#postsObject (readonly)

Returns the value of attribute posts.



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

def posts
  @posts
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



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

def total_pages
  @total_pages
end

Instance Method Details

#first?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/abqari/paginator.rb', line 14

def first?
  page == 1
end

#last?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/abqari/paginator.rb', line 18

def last?
  page == total_pages
end

#next_urlObject



29
30
31
32
33
# File 'lib/abqari/paginator.rb', line 29

def next_url
  return nil if last?

  "#{base_url}page/#{page + 1}/"
end

#prev_urlObject



22
23
24
25
26
27
# File 'lib/abqari/paginator.rb', line 22

def prev_url
  return nil if first?
  return base_url if page == 2

  "#{base_url}page/#{page - 1}/"
end