Class: Wee::Pager

Inherits:
Component show all
Defined in:
lib/wee/components/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Component

#add_decoration, #backtrack_state, #backtrack_state_chain, #decoration, #decoration=, #do_render_chain, #each_decoration, #process_callbacks, #process_callbacks_chain, #remove_decoration, #remove_decoration_if

Methods inherited from Presenter

#backtrack_state, #do_render, #get_property, #lookup_property, #process_callbacks, #properties, #properties=, #session, template, uses_property

Constructor Details

#initialize(num_entries = 0) {|_self| ... } ⇒ Pager

Returns a new instance of Pager.

Yields:

  • (_self)

Yield Parameters:

  • _self (Wee::Pager)

    the object that the method was called on



5
6
7
8
9
10
11
# File 'lib/wee/components/pager.rb', line 5

def initialize(num_entries=0)
  super()
  @num_entries = num_entries
  @current_page = 0
  @entries_per_page = 20
  yield self if block_given?
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



3
4
5
# File 'lib/wee/components/pager.rb', line 3

def current_page
  @current_page
end

#entries_per_pageObject

Returns the value of attribute entries_per_page.



2
3
4
# File 'lib/wee/components/pager.rb', line 2

def entries_per_page
  @entries_per_page
end

#num_entriesObject

Returns the value of attribute num_entries.



2
3
4
# File 'lib/wee/components/pager.rb', line 2

def num_entries
  @num_entries
end

Instance Method Details

#current_start_indexObject

Returns the index of the first entry on the current page



22
23
24
# File 'lib/wee/components/pager.rb', line 22

def current_start_index
  @current_page * @entries_per_page 
end

#firstObject

Go to first page



34
35
36
# File 'lib/wee/components/pager.rb', line 34

def first
  goto(0)
end

#goto(page) ⇒ Object

Go to page with index page Note that page-indices start with zero!



59
60
61
62
# File 'lib/wee/components/pager.rb', line 59

def goto(page)
  @current_page = page
  validate
end

#lastObject

Go to last page



40
41
42
# File 'lib/wee/components/pager.rb', line 40

def last
  goto(last_page_index())
end

#last_page_indexObject

Returns the index of the last page



28
29
30
# File 'lib/wee/components/pager.rb', line 28

def last_page_index
  num_pages() - 1
end

#nextObject

Go to next page



52
53
54
# File 'lib/wee/components/pager.rb', line 52

def next
  goto(@current_page + 1)
end

#num_pagesObject

Returns the number of pages



15
16
17
18
# File 'lib/wee/components/pager.rb', line 15

def num_pages
  n, rest = @num_entries.divmod(@entries_per_page)
  if rest > 0 then n + 1 else n end
end

#prevObject

Go to previous page



46
47
48
# File 'lib/wee/components/pager.rb', line 46

def prev
  goto(@current_page - 1)
end

#renderObject



64
65
66
67
68
69
70
71
# File 'lib/wee/components/pager.rb', line 64

def render
  return if num_pages() <= 0
  render_arrow(:first, "<<", "Go to first page"); r.space(2)
  render_arrow(:prev, "<", "Go to previous page"); r.space(2)
  render_index; r.space(2)
  render_arrow(:next, ">", "Go to next page"); r.space(2)
  render_arrow(:last, ">>", "Go to last page")
end