Class: Apes::Validators::BooleanValidator

Inherits:
BaseValidator
  • Object
show all
Defined in:
lib/apes/validators.rb

Overview

Validates boolean values.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from BaseValidator

#validate_each

Constructor Details

- (Apes::Validators::BooleanValidator) initialize(options)

Creates a new validator.

Parameters:

  • options (Hash)

    The options for the validations.



120
121
122
# File 'lib/apes/validators.rb', line 120

def initialize(options)
  super(options.reverse_merge(default_message: "must be a valid truthy/falsey value"))
end

Class Method Details

+ (Boolean|NilClass) parse(value, raise_errors: false)

Parses a boolean value.

Parameters:

  • value (Object)

    The value to parse.

  • raise_errors (Boolean)

    Whether to raise errors in case the value couldn’t be parsed.

Returns:

  • (Boolean|NilClass)

    A boolean value if parsing succeded, nil otherwise.

Raises:

  • (ArgumentError)


111
112
113
114
# File 'lib/apes/validators.rb', line 111

def self.parse(value, raise_errors: false)
  raise(ArgumentError, "Invalid boolean value \"#{value}\".") if !value.nil? && !value.boolean? && raise_errors
  value.to_boolean
end

Instance Method Details

- (Boolean) check_valid?(value)

Checks if the value is valid for this validator.

Parameters:

  • value (Object)

    The value to validate.

Returns:

  • (Boolean)

    true if the value is valid, false otherwise.



128
129
130
# File 'lib/apes/validators.rb', line 128

def check_valid?(value)
  value.blank? || value.boolean?
end