Sha256: 134d84e7dcd3423f64638dfdf7ff1fca65de7d003de95cdac388ce58dcb82a59

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

module Shamu
  module Entities
    class ListScope

      # Include paging parsing and attributes. Adds the following attributes
      # to the list scope:
      #
      # ```
      # class UsersListScope < Shamu::Entities::ListScope
      #   include Shamu::Entities::ListScope::Paging
      # end
      #
      # scope = UsersListScope.coerce!( params )
      # scope.page      # => 1
      # scope.per_page # => 25
      # ```
      module Paging

        # ============================================================================
        # @!group Attributes
        #

        # @!attribute page
        # @return [Integer] the current page number. 1 based.

        # @!attribute per_page
        # @return [Integer] number of records per page.

        # @!attribute default_per_page
        # @return [Integer] default number of records per page if not provided.

        #
        # @!endgroup Attributes

        def self.included( base )
          super

          base.attribute :page, coerce: :to_i
          base.attribute :per_page, coerce: :to_i, default: ->() { default_per_page }
          base.attribute :default_per_page, coerce: :to_i, default: 25, serialize: false
        end

        # @return [Boolean] true if the scope is paged.
        def paged?
          !!page
        end

      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
shamu-0.0.13 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.11 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.9 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.8 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.7 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.5 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.4 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.3 lib/shamu/entities/list_scope/paging.rb
shamu-0.0.2 lib/shamu/entities/list_scope/paging.rb