Class: ImHungry::Paginate

Inherits:
Object
  • Object
show all
Defined in:
lib/im_hungry/paginate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, per_page) ⇒ Paginate

Returns a new instance of Paginate.



5
6
7
8
# File 'lib/im_hungry/paginate.rb', line 5

def initialize(array, per_page)
  self.array = array
  self.per_page = per_page
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



3
4
5
# File 'lib/im_hungry/paginate.rb', line 3

def array
  @array
end

#per_pageObject

Returns the value of attribute per_page.



3
4
5
# File 'lib/im_hungry/paginate.rb', line 3

def per_page
  @per_page
end

Instance Method Details

#page_lines(page, i) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/im_hungry/paginate.rb', line 14

def page_lines(page, i)
  [
    '',
    "Page #{i + 1} of #{pages.length}",
    page.map(&:to_s)
  ]
end

#pagesObject



10
11
12
# File 'lib/im_hungry/paginate.rb', line 10

def pages
  @pages ||= array.each_slice(per_page).to_a
end


26
27
28
29
30
31
32
33
34
35
# File 'lib/im_hungry/paginate.rb', line 26

def print
  puts
  puts 'Here is what is open:'
  pages.each_with_index do |page, i|
    print_page(page, i)

    last_page = i == pages.length - 1
    Readline.readline('Next page?', true) unless last_page
  end
end


22
23
24
# File 'lib/im_hungry/paginate.rb', line 22

def print_page(page, i)
  puts page_lines(page, i)
end