Sha256: c8a103f831bfc3480de45d95c982b5e994eb888933e318f8a0ea567c04390e5f

Contents?: true

Size: 1.9 KB

Versions: 37

Compression:

Stored size: 1.9 KB

Contents

# ~*~ encoding: utf-8 ~*~
module Gollum
  module Pagination
    def self.included(klass)
      klass.extend ClassMethods
      class << klass
        # Default Integer max count of items to return in git commands.
        attr_accessor :per_page
      end
      klass.per_page = 30
    end

    module ClassMethods
      # Turns a page number into an offset number for the git skip option.
      #
      # page - Integer page number.
      #
      # Returns an Integer.
      def page_to_skip(page)
        ([1, page.to_i].max - 1) * per_page
      end

      # Fills in git-specific options for the log command using simple
      # pagination options.
      #
      # options - Hash of options:
      #           page - Optional Integer page number (default: 1)
      #           per_page - Optional Integer max count of items to return.
      #                      Defaults to #per_class class method.
      #
      # Returns Hash with :max_count and :skip keys.
      def log_pagination_options(options = {})
        skip = page_to_skip(options.delete(:page))
        options[:max_count] = [options.delete(:per_page).to_i, per_page].max
        options[:skip]      = skip if skip > 0
        options
      end
    end

    # Turns a page number into an offset number for the git skip option.
    #
    # page - Integer page number.
    #
    # Returns an Integer.
    def page_to_skip(page)
      self.class.page_to_skip(page)
    end

    # Fills in git-specific options for the log command using simple
    # pagination options.
    #
    # options - Hash of options:
    #           page - Optional Integer page number (default: 1)
    #           per_page - Optional Integer max count of items to return.
    #                      Defaults to #per_class class method.
    #
    # Returns Hash with :max_count and :skip keys.
    def log_pagination_options(options = {})
      self.class.log_pagination_options(options)
    end
  end
end

Version data entries

37 entries across 37 versions & 3 rubygems

Version Path
gollum-lib-2.0.0 lib/gollum-lib/pagination.rb
gitlab-gollum-lib-1.1.0 lib/gollum-lib/pagination.rb
gitlab-gollum-lib-1.0.2 lib/gollum-lib/pagination.rb
gollum-lib-1.0.9 lib/gollum-lib/pagination.rb
gollum-lib-1.0.8 lib/gollum-lib/pagination.rb
gollum-lib-1.0.7 lib/gollum-lib/pagination.rb
gollum-lib-1.0.6 lib/gollum-lib/pagination.rb
gollum-lib-1.0.5 lib/gollum-lib/pagination.rb
gollum-lib-1.0.4 lib/gollum-lib/pagination.rb
gitlab-gollum-lib-1.0.1 lib/gollum-lib/pagination.rb
gollum-lib-1.0.3 lib/gollum-lib/pagination.rb
gollum-lib-1.0.2 lib/gollum-lib/pagination.rb
gollum-lib-1.0.1 lib/gollum-lib/pagination.rb
gitlab-gollum-lib-1.0.0 lib/gollum-lib/pagination.rb
gollum-lib-1.0.0 lib/gollum-lib/pagination.rb
gollum-lib-0.0.1 lib/gollum-lib/pagination.rb
gollum-2.4.11 lib/gollum/pagination.rb
gollum-2.4.10 lib/gollum/pagination.rb
gollum-2.4.9 lib/gollum/pagination.rb
gollum-2.4.8 lib/gollum/pagination.rb