Sha256: 5af2dbcd7434d11e321b38e1969e38126d42edf82198cde4d6e4b25cb3a50b30

Contents?: true

Size: 1018 Bytes

Versions: 4

Compression:

Stored size: 1018 Bytes

Contents

module RestFtpDaemon
  class Paginate

    attr_writer :only

    def initialize data
      # Defaults
      @pages = 0
      @total = 0
      @data = []
      @only = nil
      @page = 1
      @pages = 1

      # Ensure data set is countable
      return unless data.is_a? Enumerable
      @data = data

      # Count elements
      @total = @data.count

      # Count pages
      @pages = (@total.to_f / DEFAULT_PAGE_SIZE).ceil
      @pages = 1 if @pages < 1
    end

    def page= raw_page
      @page = [1, raw_page.to_i, @pages].sort[1]
    end

    def browser
      out = []
      1.upto(@pages) do |p|
        out << link(p)
      end
      out.join()
    end

    def subset
      size = DEFAULT_PAGE_SIZE.to_i
      offset = (@page-1) * size
      @data[offset, size]
    end

  private

    def link p
      klass = (p == @page)? " btn-info" : ""

      "<a class='paginate btn btn-default%s' href='?only=%s&page=%d'>%p</a>" % [
        klass,
        @only,
        p,
        p
      ]
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rest-ftp-daemon-0.242.3 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.242.2 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.242.1 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.242.0 lib/rest-ftp-daemon/paginate.rb