Sha256: 6691585c81f6f06900b0cd97d9774d0cb877328fadfa151239c7f6194459af91

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

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

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

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

          private

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
serega-0.21.0 lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb
serega-0.20.1 lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb
serega-0.20.0 lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb
serega-0.19.0 lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb
serega-0.18.0 lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb