Class: Gumdrop::Util::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/gumdrop/util/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(articles, base_path = "/page", page_size = 5) ⇒ Pager

Returns a new instance of Pager.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gumdrop/util/pager.rb', line 6

def initialize(articles, base_path="/page", page_size=5)
  @all= articles
  @page_size= page_size
  @base_path= base_path
  @page_sets= @all.in_groups_of(page_size, false)
  @pages= []
  @current_page=1
  @page_sets.each do |art_ary|
    @pages << HashObject.from({
      items: art_ary,
      page: @current_page,
      uri: "#{base_path}/#{current_page}",
      pager: self
    })
    @current_page += 1
  end
  @current_page= nil
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



4
5
6
# File 'lib/gumdrop/util/pager.rb', line 4

def all
  @all
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



4
5
6
# File 'lib/gumdrop/util/pager.rb', line 4

def base_path
  @base_path
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'lib/gumdrop/util/pager.rb', line 4

def current_page
  @current_page
end

#page_setsObject (readonly)

Returns the value of attribute page_sets.



4
5
6
# File 'lib/gumdrop/util/pager.rb', line 4

def page_sets
  @page_sets
end

#pagesObject (readonly)

Returns the value of attribute pages.



4
5
6
# File 'lib/gumdrop/util/pager.rb', line 4

def pages
  @pages
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'lib/gumdrop/util/pager.rb', line 59

def [](key)
  @pages[key]
end

#currentObject



25
26
27
# File 'lib/gumdrop/util/pager.rb', line 25

def current
  @pages.fetch(@current_page, nil)
end

#eachObject



41
42
43
44
45
46
47
48
# File 'lib/gumdrop/util/pager.rb', line 41

def each
  @current_page=1
  @pages.each do |page_set|
    yield page_set
    @current_page += 1
  end
  @current_page= nil
end

#each_with_indexObject



50
51
52
53
54
55
56
57
# File 'lib/gumdrop/util/pager.rb', line 50

def each_with_index
  @current_page=1
  @pages.each do |page_set|
    yield page_set, @current_page - 1
    @current_page += 1
  end
  @current_page= nil
end

#firstObject



33
34
35
# File 'lib/gumdrop/util/pager.rb', line 33

def first
  @pages.first
end

#lastObject



37
38
39
# File 'lib/gumdrop/util/pager.rb', line 37

def last
  @pages.last
end

#lengthObject



29
30
31
# File 'lib/gumdrop/util/pager.rb', line 29

def length
  @pages.length
end