Skip to content

Commit

Permalink
Add a paginate class method
Browse files Browse the repository at this point in the history
  • Loading branch information
waferbaby committed Jan 19, 2024
1 parent ce4c6ff commit 19028f4
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/dimples/pager.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# frozen_string_literal: true

module Dimples
# A class for paginating over a group of posts.
class Pager
PER_PAGE = 5

include Enumerable

attr_reader :current_page, :previous_page, :next_page, :page_count

def initialize(url, posts, options = {})
def self.paginate(url:, posts:, options: {}, &block)
new(url: url, posts: posts, options: options).each(&block)
end

def initialize(url:, posts:, options: {})
@url = url
@posts = posts
@per_page = options.fetch(:per_page, PER_PAGE)
Expand All @@ -18,8 +23,8 @@ def initialize(url, posts, options = {})
step_to(1)
end

def each(&block)
(1..@page_count).each { |index| block.yield step_to(index) }
def each
(1..@page_count).each { |index| yield step_to(index) }
end

def step_to(page)
Expand Down

0 comments on commit 19028f4

Please sign in to comment.