class Apes::Validators::BooleanValidator

Validates boolean values.

Public Class Methods

new(options) click to toggle source

Creates a new validator.

@param options [Hash] The options for the validations. @return [Apes::Validators::BooleanValidator] A new validator.

Calls superclass method
# File lib/apes/validators.rb, line 120
def initialize(options)
  super(options.reverse_merge(default_message: "must be a valid truthy/falsey value"))
end
parse(value, raise_errors: false) click to toggle source

Parses a boolean value.

@param value [Object] The value to parse. @param raise_errors [Boolean] Whether to raise errors in case the value couldn't be parsed. @return [Boolean|NilClass] A boolean value if parsing succeded, `nil` otherwise.

# 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

Public Instance Methods

check_valid?(value) click to toggle source

Checks if the value is valid for this validator.

@param value [Object] The value to validate. @return [Boolean] `true` if the value is valid, false otherwise.

# File lib/apes/validators.rb, line 128
def check_valid?(value)
  value.blank? || value.boolean?
end