Sha256: d4adae8317ee2430096d50ea5e2025c2a749b8ead0c7d694431fea7537fa4922

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaValidations
    module Attribute
      #
      # Attribute `:value` option validator
      #
      class CheckOptValue
        class << self
          #
          # Checks attribute :value option
          #
          # @param opts [Hash] Attribute options
          #
          # @raise [SeregaError] SeregaError that option has invalid value
          #
          # @return [void]
          #
          def call(opts, block = nil)
            return unless opts.key?(:value)

            check_usage_with_other_params(opts, block)

            check_value(opts[:value])
          end

          private

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

          def check_value(value)
            check_value_type(value)

            SeregaValidations::Utils::CheckExtraKeywordArg.call(value, ":value option")
            params_count = SeregaUtils::ParamsCount.call(value, max_count: 2)

            raise SeregaError, params_count_error if params_count > 2
          end

          def check_value_type(value)
            raise SeregaError, type_error if !value.is_a?(Proc) && !value.respond_to?(:call)
          end

          def type_error
            "Option :value value must be a Proc or respond to #call"
          end

          def params_count_error
            "Option :value value can have maximum 2 parameters (object, context)"
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
serega-0.21.0 lib/serega/validations/attribute/check_opt_value.rb
serega-0.20.1 lib/serega/validations/attribute/check_opt_value.rb
serega-0.20.0 lib/serega/validations/attribute/check_opt_value.rb
serega-0.19.0 lib/serega/validations/attribute/check_opt_value.rb
serega-0.18.0 lib/serega/validations/attribute/check_opt_value.rb
serega-0.17.0 lib/serega/validations/attribute/check_opt_value.rb