Class: Pagify::BasicPage

Inherits:
Object
  • Object
show all
Defined in:
lib/pagify/page/basic.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

Instance Method Summary collapse

Constructor Details

#initialize(pager, page) ⇒ BasicPage

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



17
# File 'lib/pagify/page/basic.rb', line 17

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.



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

def method_missing msg, *args, &block
  if data.respond_to?(msg)
    data.__send__(msg, *args, &block)
  else
    super(msg, *args, &block)
  end
end

Instance Attribute Details

#pageObject (readonly)

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



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

def page
  @page
end

#pagerObject (readonly)

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



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

def pager
  @pager
end

Instance Method Details

#==(rhs) ⇒ Object

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



27
28
29
30
31
# File 'lib/pagify/page/basic.rb', line 27

def == rhs
  rhs.kind_of?(BasicPage) &&
  page  == rhs.page       &&
  pager == rhs.pager
end

#beginObject

the real beginning index



40
# File 'lib/pagify/page/basic.rb', line 40

def begin; pager.offset page; end

#dataObject

get the data fetched from pager



37
# File 'lib/pagify/page/basic.rb', line 37

def data; @data ||= fetch; end

#endObject

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



43
# File 'lib/pagify/page/basic.rb', line 43

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

#fetchObject

explicitly fetch the data from pager



34
# File 'lib/pagify/page/basic.rb', line 34

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

#nextObject

return the page instance next to this page



20
# File 'lib/pagify/page/basic.rb', line 20

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

#prevObject

return the page instance prev to this page



23
# File 'lib/pagify/page/basic.rb', line 23

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