Sha256: 7038571cddee8512a34bd0d5f5a8f58b6aff4c1c8b510eba75b074eda0a5421a

Contents?: true

Size: 786 Bytes

Versions: 6

Compression:

Stored size: 786 Bytes

Contents

# frozen_string_literal: true

require_relative '../adapter_base'
require_relative '../processor_base'

class ActiveSet
  class Processor::Paginate < Processor::Base
    class EnumerableAdapter < Adapter::Base
      def process
        return [] if @set.count <= page_size && page_number > 1

        @set[page_start..page_end] || []
      end

      private

      def page_start
        return 0 if page_number == 1

        page_size * (page_number - 1)
      end

      def page_end
        return page_start if page_size == 1

        page_start + page_size - 1
      end

      def page_size
        @instructions.get('size').to_i
      end

      def page_number
        num = @instructions.get('page').to_i
        (num.to_i - 1).negative? ? 0 : num
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activeset-0.6.5 lib/active_set/processor_paginate/enumerable_adapter.rb
activeset-0.6.4 lib/active_set/processor_paginate/enumerable_adapter.rb
activeset-0.6.3 lib/active_set/processor_paginate/enumerable_adapter.rb
activeset-0.6.2 lib/active_set/processor_paginate/enumerable_adapter.rb
activeset-0.6.1 lib/active_set/processor_paginate/enumerable_adapter.rb
activeset-0.6.0 lib/active_set/processor_paginate/enumerable_adapter.rb