Class: PostgresqlWeb::Paginator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, page, per_page, total_records) ⇒ Paginator

Returns a new instance of Paginator.



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

def initialize(data, page, per_page, total_records)
  @data = data
  @page = page.to_i
  @per_page = per_page.to_i
  @total_records = total_records.to_i
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#total_recordsObject (readonly)

Returns the value of attribute total_records.



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

def total_records
  @total_records
end

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/postgresql_web/paginator.rb', line 28

def first_page?
  page.to_i <= 1
end

#last_page?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/postgresql_web/paginator.rb', line 31

def last_page?
  page == total_pages
end

#offsetObject



12
13
14
15
16
17
18
# File 'lib/postgresql_web/paginator.rb', line 12

def offset
  if page > 0
    self.per_page * ( page - 1)
  else
    0
  end
end

#total_pagesObject



20
21
22
23
24
25
26
# File 'lib/postgresql_web/paginator.rb', line 20

def total_pages
  if self.total_records > 0
    (self.total_records.to_f / self.per_page).ceil 
  else
    1
  end
end