Sha256: 171500b19479da0320933c320dff42c5bbe3f2a056524234278a3a3da00ea3d6

Contents?: true

Size: 971 Bytes

Versions: 2

Compression:

Stored size: 971 Bytes

Contents

# frozen_string_literal: true

module JSOM
  module Pagination
    class Paginator
      def call(collection, params:, base_url: '')
        @page = Page.new(params)

        pagy, records = pagy_custom(collection, page: page)

        meta = MetaData.new(total: pagy.count, pages: pagy.pages)
        # pagy methods: :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next
        links = Links.new(page: page, total_pages: pagy.pages, url: base_url)
        Collection.new(items: records, links: links, meta: meta)
      end

      private

      attr_reader :page

      def pagy_custom(collection, page:)
        pagy = Pagy.new(count: collection.size, page: page.number, items: page.size)
        paginated =
          if collection.is_a?(Array)
            collection[pagy.offset, pagy.items]
          else
            collection.offset(pagy.offset).limit(pagy.items).to_a
          end
        [pagy, paginated]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jsom-pagination-1.0.0 lib/jsom/pagination/paginator.rb
jsom-pagination-0.2.0 lib/jsom/pagination/paginator.rb