Class: DataMapper::Paginator::ControlHelper::Sliding

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Paginator::ControlHelperAbstract
Defined in:
lib/dm-paginator/control/sliding.rb

Direct Known Subclasses

Elastic

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataMapper::Paginator::ControlHelperAbstract

#normalize_page_number, #pages_in_range

Constructor Details

#initialize(paginator, options = {}) ⇒ Sliding

Returns a new instance of Sliding.



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

def initialize paginator, options = {}
  if !paginator.is_a?( Main )
    raise ArgumentError, "paginator argument is not an instance of Main"
  end

  @paginator = paginator
  @options = options
  @options[:page_range] = options[:page_range].to_i || DataMapper::Paginator::default[:page_range]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#paginatorObject (readonly)

Returns the value of attribute paginator.



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

def paginator
  @paginator
end

Instance Method Details

#pages(page_range = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dm-paginator/control/sliding.rb', line 19

def pages page_range = nil
  return unless paginator.page_count > 0
  
  page_range = options[:page_range] || page_range

  if page_range > paginator.count
    page_range = paginator.count
  end

  delta = ( page_range / 2 ).ceil

  if paginator.page - delta > paginator.count - page_range
    lower = paginator.count - page_range + 1
    upper = paginator.count
  else
    if paginator.page - delta < 0
      delta = paginator.page
    end

    offset = paginator.page - delta
    lower = offeset + 1
    upper = offset + page_range
  end

  return pages_in_range lower, upper
end