Sha256: bd37a2ec23667b90c539e6b78a74391add7b916f8995b18242d28f01c817ff81

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Metadata
      class MetaAttribute
        class CheckBlock
          ALLOWED_PARAM_TYPES = %i[opt req]
          private_constant :ALLOWED_PARAM_TYPES

          class << self
            #
            # Checks block provided with attribute
            # Block must have up to two arguments - object and context.
            # It should not have any *rest or **key arguments
            #
            # @example without arguments
            #   metadata(:version) { CONSTANT_VERSION }
            #
            # @example with one argument
            #   metadata(:paging) { |scope| { { page: scope.page, per_page: scope.per_page, total_count: scope.total_count } }
            #
            # @example with two arguments
            #   metadata(:paging) { |scope, context| { { ... } if context[:with_paging] }
            #
            # @param block [Proc] Block that returns serialized meta attribute value
            #
            # @raise [SeregaError] SeregaError that block has invalid arguments
            #
            # @return [void]
            #
            def call(block)
              raise SeregaError, "Block must be provided when defining meta attribute" unless block

              params = block.parameters
              return if (params.count <= 2) && params.all? { |par| ALLOWED_PARAM_TYPES.include?(par[0]) }

              raise SeregaError, "Block can have maximum 2 regular parameters (no **keyword or *array args)"
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
serega-0.6.1 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.6.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.5.2 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.5.1 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.5.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.4.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.3.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.2.0 lib/serega/plugins/metadata/validations/check_block.rb