Class: CMDx::ValidatorRegistry

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(registry = nil) ⇒ ValidatorRegistry

Initialize a new validator registry with default validators.

Parameters:

  • registry (Hash, nil) (defaults to: nil)

    Optional hash mapping validator names to validator classes

Rbs:

  • (?Hash[Symbol, Class]? registry) -> void



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.

Examples:

registry.deregister(:format)
registry.deregister("presence")

Parameters:

  • name (String, Symbol)

    The name of the validator to remove

Returns:

Rbs:

  • ((String | Symbol) name) -> self



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.

Parameters:

Rbs:

  • (ValidatorRegistry source) -> void



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.

Examples:

registry.register(:custom, CustomValidator)
registry.register("email", EmailValidator)

Parameters:

  • name (String, Symbol)

    The name to register the validator under

  • validator (Class)

    The validator class to register

Returns:

Rbs:

  • ((String | Symbol) name, Class validator) -> self



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

#registryHash{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.

Examples:

registry.registry # => { presence: Validators::Presence, format: Validators::Format }

Returns:

  • (Hash{Symbol => Class})

    Hash of validator type names to validator classes

Rbs:

  • () -> Hash[Symbol, Class]



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.

Examples:

registry.validate(:presence, task, user.name, presence: true)
registry.validate(:length, task, password, { min: 8, allow_nil: false })

Parameters:

  • type (Symbol)

    The type of validator to use

  • task (Task)

    The task context for validation

  • value (Object)

    The value to validate

  • options (Hash, Object) (defaults to: EMPTY_HASH)

    Validation options or condition

Options Hash (options):

  • :allow_nil (Boolean)

    Whether to allow nil values

Raises:

  • (TypeError)

    When the validator type is not registered

Rbs:

  • (Symbol type, Task task, untyped value, untyped options) -> untyped



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, options = EMPTY_HASH)
  raise TypeError, "unknown validator type #{type.inspect}" unless registry.key?(type)

  match =
    if options.is_a?(Hash)
      case options
      in allow_nil: then !(allow_nil && value.nil?)
      else Utils::Condition.evaluate(task, options, value)
      end
    else
      options
    end

  return unless match

  Utils::Call.invoke(task, registry[type], value, options)
end