Class: Paginator

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

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ Paginator

Returns a new instance of Paginator.



7
8
9
10
11
# File 'lib/chid/paginator.rb', line 7

def initialize(list)
    @current_page = 1
    @per_page     = 3
    @list         = list.freeze
end

Instance Method Details

#paginate(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chid/paginator.rb', line 13

def paginate(&block)
    begin_index = (current_page - 1) * per_page
    end_index   = (current_page * per_page) - 1
    
    return if list.size < begin_index

    paginated_list = list.slice(begin_index..end_index)

    paginated_list.each do |object|
        block.(object) if block_given?
    end

    ask_action(&block)
end