Sha256: acb2f3bc4b4129b8bbb17e3deaf84a5feb42f1c4b24c656e2b1e23cea4ad2b93

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Batch
      #
      # Attribute `:batch` option validator
      #
      class CheckOptBatch
        class << self
          #
          # Checks attribute :batch  option
          #
          # @param opts [Hash] Attribute options
          #
          # @raise [SeregaError] Attribute validation error
          #
          # @return [void]
          #
          def call(opts, block)
            return unless opts.key?(:batch)

            SeregaValidations::Utils::CheckOptIsHash.call(opts, :batch)

            batch = opts[:batch]
            SeregaValidations::Utils::CheckAllowedKeys.call(batch, %i[key loader default])

            check_batch_opt_key(batch[:key])
            check_batch_opt_loader(batch[:loader])

            check_usage_with_other_params(opts, block)
          end

          private

          def check_batch_opt_key(key)
            raise SeregaError, "Option :key must present inside :batch option" unless key

            CheckBatchOptKey.call(key)
          end

          def check_batch_opt_loader(loader)
            raise SeregaError, "Option :loader must present inside :batch option" unless loader

            CheckBatchOptLoader.call(loader)
          end

          def check_usage_with_other_params(opts, block)
            raise SeregaError, "Option :batch can not be used together with option :key" if opts.key?(:key)
            raise SeregaError, "Option :batch can not be used together with option :value" if opts.key?(:value)
            raise SeregaError, "Option :batch can not be used together with option :const" if opts.key?(:const)
            raise SeregaError, "Option :batch can not be used together with option :delegate" if opts.key?(:delegate)
            raise SeregaError, "Option :batch can not be used together with block" if block
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
serega-0.10.0 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
serega-0.9.0 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
serega-0.8.3 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
serega-0.8.2 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
serega-0.8.1 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
serega-0.8.0 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
serega-0.7.0 lib/serega/plugins/batch/lib/validations/check_opt_batch.rb