Class: DataMapper::Paginator::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-paginator/main.rb

Overview

Main class, this object handle pagination attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Main

Returns a new instance of Main.



10
11
12
13
14
15
16
17
18
# File 'lib/dm-paginator/main.rb', line 10

def initialize options = {}
  @count = options[:count].to_i
  @page = options[:page].to_i
  @limit = options[:limit].to_i
  @offset = options[:offset].to_i
  @page_count = calculate_page_count
  @next_page = page.to_i + 1 unless page.to_i + 1 >= page_count
  @previous_page = page.to_i - 1 unless page.to_i - 1 <= 1
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def count
  @count
end

#limitObject (readonly)

Returns the value of attribute limit.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def limit
  @limit
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def next_page
  @next_page
end

#offsetObject (readonly)

Returns the value of attribute offset.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def offset
  @offset
end

#pageObject (readonly)

Returns the value of attribute page.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def page
  @page
end

#page_countObject (readonly)

Returns the value of attribute page_count.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def page_count
  @page_count
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



7
8
9
# File 'lib/dm-paginator/main.rb', line 7

def previous_page
  @previous_page
end

Instance Method Details

#pages(kind, options = {}) ⇒ Object

Get pages range using a pagination control style.

Parameters:

  • kind (String)


44
45
46
47
# File 'lib/dm-paginator/main.rb', line 44

def pages kind, options = {}
  control = DataMapper::Paginator::Control.factory self, kind.to_s, options
  control.pages
end

#to_html(kind, erb, options = {}) ⇒ String

Draw pagination controls using a partial (whatever erb file).

Parameters:

  • kind (String)
  • erb (String)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dm-paginator/main.rb', line 27

def to_html kind, erb, options = {}
  if !File.file?( erb )
    raise IOError, "erb files doesn't exists"
  end

  template = ERB.new File.read( erb ), 0, "%<>"

  control = DataMapper::Paginator::Control.factory self, kind.to_s, options
  @pages = control.pages
  template.result binding
end