Sha256: 68881aa74fc9464fbc48783d1520036395d197a1785c3f155954712ebe96a233

Contents?: true

Size: 981 Bytes

Versions: 2

Compression:

Stored size: 981 Bytes

Contents

module PageUp
  class Pages < SimpleDelegator

    def initialize collection, page, per_page
      @page     = parse page, 1
      @per_page = parse per_page, 25
      @origin   = collection
      super collection[page_range]
    end

    def pages
      (@origin.size.to_f / per_page).ceil
    end

    def current_slice
      slice_start..slice_end
    end

    attr_reader :page, :per_page

    private
      def page_range
        current_page...current_page+per_page
      end

      def current_page
        (page-1)*per_page
      end

      def parse value, default
        Integer(value)
      rescue TypeError
        default
      end

      def slice_start
        if page - 2 <= 0
          1
        elsif page + 2 > pages
          pages - 4
        else
          page - 2
        end
      end

      def slice_end
        if page + 2 > pages
          pages
        elsif page - 2 <= 0
          5
        else
          page + 2
        end
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
page_up-0.0.2 lib/page_up/pages.rb
page_up-0.0.1 lib/page_up/pages.rb