Sha256: 7df9a3f44117ef7861442a176f5a871992dc468abc38b2d39dbbc848948726ce

Contents?: true

Size: 1.69 KB

Versions: 17

Compression:

Stored size: 1.69 KB

Contents

module Shamu
  module Entities
    class ListScope

      # Include paging parsing and attributes exposed as a nested page object.
      # Adds the following attributes to the list scope:
      #
      # ```
      # class UsersListScope < Shamu::Entities::ListScope
      #   include Shamu::Entities::ListScope::ScopedPaging
      # end
      #
      # scope = UsersListScope.coerce!( page: { number: 5, size: 50 } )
      # scope.page.number # => 5
      # scope.page.size   # => 50
      # ```
      module ScopedPaging

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

        # @!attribute page
        # @return [PageScope] the paging scope.

        #
        # @!endgroup Attributes

        def self.included( base )
          super

          base.attribute :page, build: PageScope, default: PageScope.new
        end

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

        # The scope of a [ScopedPaging] list scope.
        class PageScope
          include Shamu::Attributes

          # @!attribute
          # @return [Integer] the page number.
          attribute :number, coerce: :to_i

          # @!attribute
          # @return [Integer] the size of each page.
          attribute :size, coerce: :to_i, default: ->() { default_size }

          # @!attribute
          # @return [Integer] the default page size if not specified.
          attribute :default_size, coerce: :to_i, default: 25, serialize: false

          # @return [Hash] the scope as a params hash.
          def params
            to_attributes
          end
        end

      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shamu-0.0.24 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.21 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.20 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.19 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.18 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.17 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.15 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.14 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.13 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.11 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.9 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.8 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.7 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.5 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.4 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.3 lib/shamu/entities/list_scope/scoped_paging.rb
shamu-0.0.2 lib/shamu/entities/list_scope/scoped_paging.rb