class Attribool::ValidatorService

A simple interface to run validators, which should implement a valid? method which returns true if conditions are valid, and an error method which returns an exception class and message to be raised when validations fail.

Public Class Methods

call(validator_name, *args) click to toggle source

Run the validator.

@param [Symbol] validator_name

@param [Object] *args to be forwarded to validator

# File lib/attribool/validator_service.rb, line 16
def self.call(validator_name, *args)
  new(::Attribool::Validators.fetch(validator_name), *args).validate
end
new(validator, *args) click to toggle source

Construct the service and inject the validator.

@param [Class] Validator

@param [Object] *args

# File lib/attribool/validator_service.rb, line 26
def initialize(validator, *args)
  @validator = validator.new(*args)
end

Public Instance Methods

validate() click to toggle source

Raises the validator’s exception unless its conditions are met.

@return [Boolean]

@raise [Exception] if validation fails

# File lib/attribool/validator_service.rb, line 36
def validate
  @validator.valid? || raise(@validator.error)
end