Sha256: 0c33eb8e594a4c0951b1114b93e90891edaf4ac33f672f92fb7247c57ef4e95c

Contents?: true

Size: 796 Bytes

Versions: 3

Compression:

Stored size: 796 Bytes

Contents

# frozen_string_literal: true

module Attribool::Validators
  ##
  # Ensures that a value is not +nil+, unless +nil+ is allowed as a value.
  class NilAttributeValidator
    ##
    # Construct the validator.
    #
    # @param [String, Symbol] ivar
    #
    # @param [Object] value
    #
    # @param [Boolean] allow_nil
    def initialize(ivar, value, allow_nil)
      @ivar = ivar
      @value = value
      @allow_nil = allow_nil
    end

    ##
    # Do we either allow values to be +nil+, or is the value not +nil+?
    #
    # @return [Boolean]
    def valid?
      @allow_nil || !@value.nil?
    end

    ##
    # The exception to raise if validations fail.
    #
    # @return [TypeError] the exception with message
    def error
      TypeError.new("#{@ivar} is nil")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attribool-2.0.5 lib/attribool/validators/nil_attribute_validator.rb
attribool-2.0.4 lib/attribool/validators/nil_attribute_validator.rb
attribool-2.0.3 lib/attribool/validators/nil_attribute_validator.rb