Sha256: e1783a51b5a0a1214bdf6df23c229f2924ae91327689d67249fcdec915b40151

Contents?: true

Size: 1.63 KB

Versions: 14

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Metadata
      class MetaAttribute
        #
        # Validator for meta_attribute block parameter
        #
        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

14 entries across 14 versions & 1 rubygems

Version Path
serega-0.16.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.15.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.14.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.12.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.11.2 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.11.1 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.11.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.10.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.9.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.8.3 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.8.2 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.8.1 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.8.0 lib/serega/plugins/metadata/validations/check_block.rb
serega-0.7.0 lib/serega/plugins/metadata/validations/check_block.rb