Class: Ludy::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ludy/paginator.rb

Overview

paginator 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 rails’ model, fetcher would be Model.find :all, :offset => offset, :limit => per_page. see ArrayPaginator and RailsPaginator, if you just simply need them or have a look at the source code for example usage for Paginator.

Direct Known Subclasses

ArrayPaginator, NullPaginator, RailsPaginator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fetcher, counter, per_page = 20) ⇒ Paginator

fetcher is function that fetch the data,

counter is function that count the data.

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



58
59
60
61
62
# File 'lib/ludy/paginator.rb', line 58

def initialize fetcher, counter, per_page = 20
  @per_page = per_page
  @fetcher = fetcher
  @counter = counter
end

Instance Attribute Details

#counterObject

Returns the value of attribute counter.



54
55
56
# File 'lib/ludy/paginator.rb', line 54

def counter
  @counter
end

#fetcherObject

Returns the value of attribute fetcher.



54
55
56
# File 'lib/ludy/paginator.rb', line 54

def fetcher
  @fetcher
end

#per_pageObject

Returns the value of attribute per_page.



54
55
56
# File 'lib/ludy/paginator.rb', line 54

def per_page
  @per_page
end

Class Method Details

.nullObject

return a null pager that stubs anything to 0



64
# File 'lib/ludy/paginator.rb', line 64

def self.null; NullPaginator.instance; end

Instance Method Details

#==(rhs) ⇒ Object

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



67
68
69
70
71
# File 'lib/ludy/paginator.rb', line 67

def == rhs
  self.per_page == rhs.per_page and
  self.fetcher  == rhs.fetcher  and
  self.counter  == rhs.counter
end

#countObject

simply call @counter.call



88
# File 'lib/ludy/paginator.rb', line 88

def count; @counter.call; end

#eachObject

for each page…



73
# File 'lib/ludy/paginator.rb', line 73

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

#offset(page) ⇒ Object

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



92
93
94
# File 'lib/ludy/paginator.rb', line 92

def offset 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.



80
81
82
83
84
# File 'lib/ludy/paginator.rb', line 80

def page page
  offset = (page-1)*@per_page
  return nil unless page > 0 and offset < count
  Page.new self, page
end

#sizeObject

return the amount of pages



86
# File 'lib/ludy/paginator.rb', line 86

def size; (count/@per_page.to_f).ceil; end

#to_aObject Also known as: pages

return all pages in an array



75
# File 'lib/ludy/paginator.rb', line 75

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