Class: CMDx::ValidatorRegistry
- Inherits:
-
Object
- Object
- CMDx::ValidatorRegistry
- Extended by:
- Forwardable
- Defined in:
- lib/cmdx/validator_registry.rb
Overview
Registry for managing validation rules and their corresponding validator classes. Provides methods to register, deregister, and execute validators against task values.
Instance Attribute Summary collapse
-
#registry ⇒ Hash{Symbol => Class}
(also: #to_h)
readonly
Returns the internal registry mapping validator types to classes.
Instance Method Summary collapse
-
#deregister(name) ⇒ ValidatorRegistry
Remove a validator from the registry by name.
-
#dup ⇒ ValidatorRegistry
Create a duplicate of the registry with copied internal state.
-
#initialize(registry = nil) ⇒ ValidatorRegistry
constructor
Initialize a new validator registry with default validators.
-
#register(name, validator) ⇒ ValidatorRegistry
Register a new validator class with the given name.
-
#validate(type, task, value, options = {}) ⇒ Object
Validate a value using the specified validator type and options.
Constructor Details
#initialize(registry = nil) ⇒ ValidatorRegistry
Initialize a new validator registry with default validators.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cmdx/validator_registry.rb', line 30 def initialize(registry = nil) @registry = registry || { exclusion: Validators::Exclusion, format: Validators::Format, inclusion: Validators::Inclusion, length: Validators::Length, numeric: Validators::Numeric, presence: Validators::Presence } end |
Instance Attribute Details
#registry ⇒ Hash{Symbol => Class} (readonly) Also known as: to_h
Returns the internal registry mapping validator types to classes.
18 19 20 |
# File 'lib/cmdx/validator_registry.rb', line 18 def registry @registry end |
Instance Method Details
#deregister(name) ⇒ ValidatorRegistry
Remove a validator from the registry by name.
78 79 80 81 |
# File 'lib/cmdx/validator_registry.rb', line 78 def deregister(name) registry.delete(name.to_sym) self end |
#dup ⇒ ValidatorRegistry
Create a duplicate of the registry with copied internal state.
46 47 48 |
# File 'lib/cmdx/validator_registry.rb', line 46 def dup self.class.new(registry.dup) end |
#register(name, validator) ⇒ ValidatorRegistry
Register a new validator class with the given name.
62 63 64 65 |
# File 'lib/cmdx/validator_registry.rb', line 62 def register(name, validator) registry[name.to_sym] = validator self end |
#validate(type, task, value, options = {}) ⇒ Object
Validate a value using the specified validator type and options.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cmdx/validator_registry.rb', line 98 def validate(type, task, value, = {}) raise TypeError, "unknown validator type #{type.inspect}" unless registry.key?(type) match = if .is_a?(Hash) case in allow_nil: then allow_nil && value.nil? else Utils::Condition.evaluate(task, , value) end else end return unless match Utils::Call.invoke(task, registry[type], value, ) end |