Module: Middleman::Pagination::ArrayHelpers

Included in:
Array
Defined in:
lib/middleman-core/core_extensions/collections/pagination.rb

Instance Method Summary collapse

Instance Method Details

#per_page(per_page) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/middleman-core/core_extensions/collections/pagination.rb', line 7

def per_page(per_page)
  return to_enum(__method__, per_page) unless block_given?

  parts = if per_page.respond_to? :call
    per_page.call(dup)
  else
    each_slice(per_page).reduce([]) do |sum, items|
      sum << items
    end
  end

  num_pages = parts.length
  collection = self

  current_start_i = 0
  parts.each_with_index do |items, i|
    num = i + 1

    meta = ::Middleman::Pagination.page_locals(
      num,
      num_pages,
      collection,
      items,
      current_start_i
    )

    yield items, num, meta, num >= num_pages

    current_start_i += items.length
  end
end