Class: Attribool::Validators::NilAttributeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/attribool/validators/nil_attribute_validator.rb

Overview

Ensures that a value is not nil, unless nil is allowed as a value.

Instance Method Summary collapse

Constructor Details

#initialize(ivar, value, allow_nil) ⇒ NilAttributeValidator

Construct the validator.

Parameters:

  • ivar (String, Symbol)
  • value (Object)
  • allow_nil (Boolean)


15
16
17
18
19
# File 'lib/attribool/validators/nil_attribute_validator.rb', line 15

def initialize(ivar, value, allow_nil)
  @ivar = ivar
  @value = value
  @allow_nil = allow_nil
end

Instance Method Details

#errorTypeError

The exception to raise if validations fail.

Returns:

  • (TypeError)

    the exception with message



33
34
35
# File 'lib/attribool/validators/nil_attribute_validator.rb', line 33

def error
  TypeError.new("#{@ivar} is nil")
end

#valid?Boolean

Do we either allow values to be nil, or is the value not nil?

Returns:

  • (Boolean)


25
26
27
# File 'lib/attribool/validators/nil_attribute_validator.rb', line 25

def valid?
  @allow_nil || !@value.nil?
end