Sha256: 39b6c2aa34353ea6771e75f7b7afeb3717c20b58268e997faba4ac11199b9e50

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module If
      #
      # Validator for attribute :unless_value option
      #
      class CheckOptUnlessValue
        class << self
          #
          # Checks attribute :unless_value option that must be [nil, Symbol, Proc, #call]
          #
          # @param opts [Hash] Attribute options
          #
          # @raise [SeregaError] Attribute validation error
          #
          # @return [void]
          #
          def call(opts)
            return unless opts.key?(:unless_value)

            check_usage_with_other_params(opts)
            check_type(opts[:unless_value])
          end

          private

          def check_usage_with_other_params(opts)
            raise SeregaError, "Option :unless_value can not be used together with option :serializer" if opts.key?(:serializer)
          end

          def check_type(value)
            return if value.is_a?(Symbol)
            raise SeregaError, must_be_callable unless value.respond_to?(:call)

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

            if params_count > 2
              raise SeregaError, "Option :unless_value value should have up to 2 parameters (value, context)"
            end
          end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
serega-0.21.0 lib/serega/plugins/if/validations/check_opt_unless_value.rb
serega-0.20.1 lib/serega/plugins/if/validations/check_opt_unless_value.rb
serega-0.20.0 lib/serega/plugins/if/validations/check_opt_unless_value.rb
serega-0.19.0 lib/serega/plugins/if/validations/check_opt_unless_value.rb
serega-0.18.0 lib/serega/plugins/if/validations/check_opt_unless_value.rb
serega-0.17.0 lib/serega/plugins/if/validations/check_opt_unless_value.rb