class Validate::Scope

Public Class Methods

current() click to toggle source
# File lib/validate/scope.rb, line 6
def self.current
  @current ||= Scope.new
end
new() click to toggle source
# File lib/validate/scope.rb, line 10
def initialize
  @constraints = {}
  @validators = {}
end

Public Instance Methods

register_validator(name, validator) click to toggle source
# File lib/validate/scope.rb, line 15
def register_validator(name, validator)
  if @validators.include?(name)
    raise Error::ArgumentError,
          "duplicate validator :#{name}"
  end

  @validators[name] = validator
end
validator(name) click to toggle source
# File lib/validate/scope.rb, line 28
def validator(name)
  validator_name.assert(name,
                        message: "invalid validator #{name.inspect}",
                        error_class: KeyError)

  @validators.fetch(name) { name.validator }
end
validator?(name) click to toggle source
# File lib/validate/scope.rb, line 24
def validator?(name)
  @validators.include?(name)
end

Private Instance Methods

validator_name() click to toggle source
# File lib/validate/scope.rb, line 38
def validator_name
  @validator_name ||= Assertions.create(@validators) do |validators|
    not_nil(message: 'name must not be nil')
    (one_of(values: validators,
            message: '%{value.inspect} must be an existing validator name') |
        respond_to(:validator,
                   message: '%{value.inspect} must respond to :validator'))
  end
end