Class: Statique::Paginator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(documents, path, page, statique = Statique.instance) ⇒ Paginator

Returns a new instance of Paginator.



11
12
13
14
15
16
17
18
19
20
# File 'lib/statique/paginator.rb', line 11

def initialize(documents, path, page, statique = Statique.instance)
  @path, @page = path, [page.to_i, 1].max

  @statique = statique

  @total_documents = documents.size
  @offset = (self.class.per_page * (@page - 1))
  @total_pages = (@total_documents.to_f / self.class.per_page).ceil
  @documents = documents[@offset, self.class.per_page]
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



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

def documents
  @documents
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#total_documentsObject (readonly)

Returns the value of attribute total_documents.



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

def total_documents
  @total_documents
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



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

def total_pages
  @total_pages
end

Class Method Details

.per_pageObject



7
8
9
# File 'lib/statique/paginator.rb', line 7

def self.per_page
  @per_page ||= 10
end

Instance Method Details

#next_pageObject



39
40
41
42
43
44
45
46
# File 'lib/statique/paginator.rb', line 39

def next_page
  page = [@total_pages, @page + 1].min
  if page == @page
    nil
  else
    page
  end
end

#next_page_pathObject



48
49
50
# File 'lib/statique/paginator.rb', line 48

def next_page_path
  page_path(next_page)
end

#per_pageObject



22
23
24
# File 'lib/statique/paginator.rb', line 22

def per_page
  self.class.per_page
end

#previous_pageObject



26
27
28
29
30
31
32
33
# File 'lib/statique/paginator.rb', line 26

def previous_page
  page = [1, @page - 1].max
  if page == @page
    nil
  else
    page
  end
end

#previous_page_pathObject



35
36
37
# File 'lib/statique/paginator.rb', line 35

def previous_page_path
  page_path(previous_page)
end