Class: Pagify::BasicPager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pagify/pager/basic.rb

Overview

pager is something help you paginate the data, through the fetcher and counter you pass in. e.g., for a array, the fetcher would be array[offset, per_page], the counter would be array.size. and for data mapper’s resource, fetcher would be Model.all :offset => offset, :limit => per_page. see ArrayPager and DataMapperPager, ActiveRecordPager, etc. if you just simply need them or have a look at the source code for example usage for Pager.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BasicPager

fetcher is function that fetch the data,

counter is function that count the data,
null_page indicates that pager sohuld

the default per_page is 20. you can reset per_page property later.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pagify/pager/basic.rb', line 30

def initialize opts = {}
  raise ArgumentError.new('missing fetcher and/or counter.') if
    !opts[:fetcher] || !opts[:counter]

  raise ArgumentError.new('fetcher or counter does not respond to call.') if
    !opts[:fetcher].respond_to?(:call) || !opts[:counter].respond_to?(:call)

  @opts     = {}
  @fetcher  = opts[:fetcher]
  @counter  = opts[:counter]

  self.per_page  = opts[:per_page]  || 20
  self.null_page = opts[:null_page] || true
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



13
14
15
# File 'lib/pagify/pager/basic.rb', line 13

def counter
  @counter
end

#fetcherObject (readonly)

Returns the value of attribute fetcher.



13
14
15
# File 'lib/pagify/pager/basic.rb', line 13

def fetcher
  @fetcher
end

#optsObject

Returns the value of attribute opts.



14
15
16
# File 'lib/pagify/pager/basic.rb', line 14

def opts
  @opts
end

Instance Method Details

#==(rhs) ⇒ Object

if two paginators are equal, then the properties of per_page, fetcher, counter are all equal.



47
48
49
50
51
52
# File 'lib/pagify/pager/basic.rb', line 47

def == rhs
  return false unless rhs
  self.per_page == rhs.per_page and
  self.fetcher  == rhs.fetcher  and
  self.counter  == rhs.counter
end

#eachObject

for each page…



55
# File 'lib/pagify/pager/basic.rb', line 55

def each; 1.upto(size){ |i| yield page(i) }; end

#entries_countObject

simply call @counter.call



90
# File 'lib/pagify/pager/basic.rb', line 90

def entries_count; counter.call; end

#offset(page) ⇒ Object

get the offset property about the page. it is simply (page-1)*@per_page



94
95
96
# File 'lib/pagify/pager/basic.rb', line 94

def offset page
  (normalize_page(page) - 1) * per_page
end

#page(page) ⇒ Object Also known as: []

create page instance by page number. if page number you specified was not existed, nil would be returned. note, page start at 1, not zero.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pagify/pager/basic.rb', line 70

def page page
  if page_exists?(page)
    return BasicPage.new(self, normalize_page(page))

  else
    if null_page
      return null_page_instance
    else
      return nil
    end

  end

end

#page_exists?(page) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/pagify/pager/basic.rb', line 61

def page_exists? page
  page = normalize_page(page)
  offset = (page - 1) * per_page
  page > 0 && offset < entries_count
end

#sizeObject

return the amount of pages



87
# File 'lib/pagify/pager/basic.rb', line 87

def size; (entries_count / per_page.to_f).ceil; end

#to_aObject Also known as: pages

return all pages in an array



58
# File 'lib/pagify/pager/basic.rb', line 58

def to_a; map{ |e| e }; end