Sha256: f41e2a33bfa9a12760d04b1aa6b06a07f1e66bff12e3347ae9928a358c936f0a

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# A module for Herald
module PuppetHerald
  # module for models
  module Models
    # Pagianation object
    class Pagination
      # Pagination headers
      KEYS = {
        page: 'X-Paginate-Page',
        limit: 'X-Paginate-Limit',
        total: 'X-Paginate-Elements',
        pages: 'X-Paginate-Pages'
      }
      # Pagination attribute limit
      # @return [Integer] pagination
      attr_reader :page, :limit, :pages, :total
      # Pagination attribute offset
      # @return [Integer] pagination
      def offset
        (page - 1) * limit
      end
      # Sets a total elements for pagination
      # @param total [Integer] a total number of elements
      # @return [nil]
      def total=(total)
        @total = total.to_i
        @pages = (@total / @limit.to_f).ceil
        nil
      end
      # A constructor
      #
      # @param page [Integer] page to fetch
      # @param limit [Integer] pagination limit
      def initialize(page, limit)
        msg = 'Invalid value for pagination'
        fail ArgumentError, "#{msg} limit - #{limit.inspect}" unless limit.to_i >= 1
        fail ArgumentError, "#{msg} page #{page.inspect}" if page.to_i < 1
        @limit = limit.to_i
        @page = page.to_i
        @total = nil
        @pages = nil
      end
      # A default pagination settings
      DEFAULT = PuppetHerald::Models::Pagination.new(1, 20)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
puppet-herald-0.8.1 lib/puppet-herald/models.rb
puppet-herald-0.8.0 lib/puppet-herald/models.rb