Class: Validate::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/validate/scope.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScope

Returns a new instance of Scope.



10
11
12
13
# File 'lib/validate/scope.rb', line 10

def initialize
  @constraints = {}
  @validators = {}
end

Class Method Details

.currentObject



6
7
8
# File 'lib/validate/scope.rb', line 6

def self.current
  @current ||= Scope.new
end

Instance Method Details

#register_validator(name, validator) ⇒ Object



15
16
17
18
19
20
21
22
# 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) ⇒ Object



28
29
30
31
32
33
34
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/validate/scope.rb', line 24

def validator?(name)
  @validators.include?(name)
end