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.
Supports copy-on-write semantics: a duped registry shares the parent’s data until a write operation triggers materialization.
Instance Method Summary collapse
-
#deregister(name) ⇒ ValidatorRegistry
Remove a validator from the registry by name.
-
#initialize(registry = nil) ⇒ ValidatorRegistry
constructor
Initialize a new validator registry with default validators.
-
#initialize_dup(source) ⇒ Object
Sets up copy-on-write state when duplicated via dup.
-
#register(name, validator) ⇒ ValidatorRegistry
Register a new validator class with the given name.
-
#registry ⇒ Hash{Symbol => Class}
(also: #to_h)
Returns the internal registry mapping validator types to classes.
-
#validate(type, task, value, options = EMPTY_HASH) ⇒ 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.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cmdx/validator_registry.rb', line 22 def initialize(registry = nil) @registry = registry || { absence: Validators::Absence, exclusion: Validators::Exclusion, format: Validators::Format, inclusion: Validators::Inclusion, length: Validators::Length, numeric: Validators::Numeric, presence: Validators::Presence } end |
Instance Method Details
#deregister(name) ⇒ ValidatorRegistry
Remove a validator from the registry by name.
89 90 91 92 93 94 |
# File 'lib/cmdx/validator_registry.rb', line 89 def deregister(name) materialize! @registry.delete(name.to_sym) self end |
#initialize_dup(source) ⇒ Object
Sets up copy-on-write state when duplicated via dup.
39 40 41 42 43 |
# File 'lib/cmdx/validator_registry.rb', line 39 def initialize_dup(source) @parent = source @registry = nil super end |
#register(name, validator) ⇒ ValidatorRegistry
Register a new validator class with the given name.
71 72 73 74 75 76 |
# File 'lib/cmdx/validator_registry.rb', line 71 def register(name, validator) materialize! @registry[name.to_sym] = validator self end |
#registry ⇒ Hash{Symbol => Class} Also known as: to_h
Returns the internal registry mapping validator types to classes. Delegates to the parent registry when not yet materialized.
54 55 56 |
# File 'lib/cmdx/validator_registry.rb', line 54 def registry @registry || @parent.registry end |
#validate(type, task, value, options = EMPTY_HASH) ⇒ Object
Validate a value using the specified validator type and options.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/cmdx/validator_registry.rb', line 111 def validate(type, task, value, = EMPTY_HASH) 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 |