Class: NeatPages::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/neat_pages/base.rb

Overview

************************************************************************************* TOCOMMENT *************************************************************************************

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_page_param, options = {}) ⇒ Base

************************************************************************************* CONSTRUCTOR *************************************************************************************



13
14
15
16
17
18
19
20
21
22
# File 'lib/neat_pages/base.rb', line 13

def initialize(current_page_param, options={})
  options = { per_page: 10, total_items: 0 }.merge(options)

  @per_page = options[:per_page].to_i
  @total_items = options[:total_items]

  init_current_page current_page_param

  @out_of_bound = init_out_of_bound
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



7
8
9
# File 'lib/neat_pages/base.rb', line 7

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



6
7
8
# File 'lib/neat_pages/base.rb', line 6

def per_page
  @per_page
end

#total_itemsObject (readonly)

Returns the value of attribute total_items.



8
9
10
# File 'lib/neat_pages/base.rb', line 8

def total_items
  @total_items
end

Instance Method Details

#empty?Boolean

************************************************************************************* PUBLIC INSTANCE METHODS *************************************************************************************



28
29
30
# File 'lib/neat_pages/base.rb', line 28

def empty?
  @total_items == 0
end

#limitObject



32
33
34
# File 'lib/neat_pages/base.rb', line 32

def limit
  @per_page
end

#next?Boolean



40
41
42
# File 'lib/neat_pages/base.rb', line 40

def next?
  @current_page < total_pages and not out_of_bound?
end

#next_pageObject



36
37
38
# File 'lib/neat_pages/base.rb', line 36

def next_page
  next? ? (@current_page + 1) : total_pages
end

#offsetObject



44
45
46
47
48
49
50
# File 'lib/neat_pages/base.rb', line 44

def offset
  if out_of_bound?
    total_items + 1
  else
    (@current_page - 1) * @per_page
  end
end

#out_of_bound?Boolean



52
53
54
# File 'lib/neat_pages/base.rb', line 52

def out_of_bound?
  @out_of_bound
end

#paginated?Boolean



56
57
58
# File 'lib/neat_pages/base.rb', line 56

def paginated?
  @total_items > @per_page
end

#previous?Boolean



64
65
66
# File 'lib/neat_pages/base.rb', line 64

def previous?
  @current_page > 1 and not out_of_bound?
end

#previous_pageObject



60
61
62
# File 'lib/neat_pages/base.rb', line 60

def previous_page
  previous? ? (@current_page - 1) : 1
end

#response_headersObject



68
69
70
71
72
73
74
75
# File 'lib/neat_pages/base.rb', line 68

def response_headers
  {
    "X-Total-Items" => @total_items.to_s,
    "X-Total-Pages" => total_pages.to_s,
    "X-Per-Page" => @per_page.to_s,
    "X-Current-Page" => @current_page.to_s
  }
end

#set_total_items(qte) ⇒ Object



77
78
79
80
# File 'lib/neat_pages/base.rb', line 77

def set_total_items(qte)
  @total_items = qte
  @out_of_bound = init_out_of_bound
end

#total_pagesObject



82
83
84
85
# File 'lib/neat_pages/base.rb', line 82

def total_pages
  total = (@total_items.to_f / @per_page).ceil
  total != 0 ? total : 1
end