Class: Ludy::Page

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

Overview

which was produced by Paginator#page / Paginator#[], representing page that contained target data. it would use lazy fetching, whenever you need the data, the fetcher would be invoked that time. whenever the data was fetched, it won’t fetch again. it you need refetch, call Page#fetch

Direct Known Subclasses

NullPage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pager, page) ⇒ Page

don’t create a page instance yourself unless you have to



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

def initialize pager, page; @pager, @page = pager, page; end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args, &block) ⇒ Object

any other method call would delegate to the data, e.g., each, to_a, map, first, method_you_defined, etc.



41
42
43
# File 'lib/ludy/paginator.rb', line 41

def method_missing msg, *args, &block
  self.data.public_send msg, *args, &block
end

Instance Attribute Details

#pageObject (readonly)

pager to get the original pager; page to get the number of this page



16
17
18
# File 'lib/ludy/paginator.rb', line 16

def page
  @page
end

#pagerObject (readonly)

pager to get the original pager; page to get the number of this page



16
17
18
# File 'lib/ludy/paginator.rb', line 16

def pager
  @pager
end

Class Method Details

.nullObject

return a null page that stubs anything to 0



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

def self.null; NullPage.instance; end

Instance Method Details

#==(rhs) ⇒ Object

if the page numbers and the pagers are equal, then the pages are equal.



27
28
29
30
# File 'lib/ludy/paginator.rb', line 27

def == rhs
  @page  == rhs.instance_variable_get('@page' ) and
  @pager == rhs.instance_variable_get('@pager')
end

#beginObject

the real beginning index



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

def begin; @pager.offset @page; end

#dataObject

get the data fetched from pager



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

def data; @data ||= fetch; end

#endObject

the real ending index (need fetch, because we don’t know the size)



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

def end; self.begin + self.size - 1; end

#fetchObject

explicitly fetch the data from pager



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

def fetch; @data = @pager.fetcher[self.begin, @pager.per_page]; end

#nextObject

return the page instance next to this page



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

def next; @pager.page(@page+1); end

#prevObject

return the page instance prev to this page



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

def prev; @pager.page(@page-1); end