Sha256: f8d56977241842d85e33d861e8a87a17bfff0abb405521566c0983dd46ebcb87

Contents?: true

Size: 1.02 KB

Versions: 9

Compression:

Stored size: 1.02 KB

Contents

module RestFtpDaemon
  class Paginate

    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 / PAGINATE_MAX).ceil
      @pages = 1 if @pages < 1
    end

    def only= raw_only
      @only = raw_only
    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 = PAGINATE_MAX.to_i
      offset = (@page-1) * size
      @data[offset, size]
    end

  private

    def link p
      klass = (p == @page)? ' btn-info' : ''
      return "<a class='page btn btn-default%s' href='?only=%s&page=%d'>%p</a>" % [
        klass,
        @only,
        p,
        p,
      ]
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rest-ftp-daemon-0.221.2 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.221.1 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.221 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.220.4 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.220.3 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.220.1 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.220.0 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.214.0 lib/rest-ftp-daemon/paginate.rb
rest-ftp-daemon-0.213.0 lib/rest-ftp-daemon/paginate.rb