Sha256: ab66c5d5f0a832275f74453f2131291d105de4f2d57c9f172e78dd05e3e7e5d2

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Batch
      #
      # Validator for option :key in attribute :batch option
      #
      class CheckBatchOptKey
        class << self
          #
          # Checks option :key of attribute :batch option
          #
          # @param key [nil, #call] Attribute :batch option :key
          #
          # @raise [SeregaError] validation error
          #
          # @return [void]
          #
          def call(key)
            return if key.is_a?(Symbol)

            raise SeregaError, must_be_callable unless key.respond_to?(:call)

            SeregaValidations::Utils::CheckExtraKeywordArg.call(key, "batch option :key")
            params_count = SeregaUtils::ParamsCount.call(key, max_count: 2)
            raise SeregaError, params_count_error if params_count > 2
          end

          private

          def params_count_error
            "Invalid :batch option :key. It can accept maximum 2 parameters (object, context)"
          end

          def must_be_callable
            "Invalid :batch option :key. It must be a Symbol, a Proc or respond to :call"
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
serega-0.17.0 lib/serega/plugins/batch/lib/validations/check_batch_opt_key.rb