Sha256: 19beb1f1e2c038b48762cf8ac9b4d0cbc81dd78ea4dc9991047855240c8a958c

Contents?: true

Size: 832 Bytes

Versions: 7

Compression:

Stored size: 832 Bytes

Contents

# frozen_string_literal: true

require_relative '../base_adapter'
require_relative '../base_processor'

class ActiveSet
  class PaginateProcessor < BaseProcessor
    class EnumerableAdapter < BaseAdapter
      def process
        return_set(paginated_set)
      end

      private

      def paginated_set
        return [] if @set.count <= page_size && page_number > 1

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

      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
        @instruction.value
      end

      def page_number
        num = @instruction.attribute.to_i
        (num.to_i - 1).negative? ? 0 : num
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activeset-0.5.8 lib/active_set/processors/paginate/enumerable_adapter.rb
activeset-0.5.7 lib/active_set/processors/paginate/enumerable_adapter.rb
activeset-0.5.6 lib/active_set/processors/paginate/enumerable_adapter.rb
activeset-0.5.5 lib/active_set/processors/paginate/enumerable_adapter.rb
activeset-0.5.4 lib/active_set/processors/paginate/enumerable_adapter.rb
activeset-0.5.3 lib/active_set/processors/paginate/enumerable_adapter.rb
activeset-0.5.2 lib/active_set/processors/paginate/enumerable_adapter.rb