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.

Instance Attribute Summary collapse

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



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

#registryHash{Symbol => Class} (readonly) Also known as: to_h

Returns the internal registry mapping validator types to classes.

Examples:

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

Returns:

  • (Hash{Symbol => Class})

    Hash of validator type names to validator 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.

Examples:

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

Parameters:

  • name (String, Symbol)

    The name of the validator to remove

Returns:



78
79
80
81
# File 'lib/cmdx/validator_registry.rb', line 78

def deregister(name)
  registry.delete(name.to_sym)
  self
end

#dupValidatorRegistry

Create a duplicate of the registry with copied internal state.

Returns:



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.

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:



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.

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: {})

    Validation options or condition

Options Hash (options):

  • :allow_nil (Boolean)

    Whether to allow nil values

Raises:

  • (TypeError)

    When the validator type is not registered



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, options = {})
  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