Class: Stannum::Constraints::Absence

Inherits:
Base
  • Object
show all
Defined in:
lib/stannum/constraints/absence.rb

Overview

An absence constraint asserts that the object is nil or empty.

Examples:

Using an Absence constraint

constraint = Stannum::Constraints::Absence.new

constraint.matches?(nil) #=> true
constraint.matches?(Object.new)  #=> false

Using a Absence constraint with an Array

constraint.matches?([])        #=> true
constraint.matches?([1, 2, 3]) #=> false

Using a Absence constraint with an Hash

constraint.matches?({})               #=> true
constraint.matches?({ key: 'value' }) #=> false

Constant Summary collapse

NEGATED_TYPE =

The :type of the error generated for a matching object.

Stannum::Constraints::Presence::TYPE
TYPE =

The :type of the error generated for a non-matching object.

Stannum::Constraints::Presence::NEGATED_TYPE

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#==, #clone, #does_not_match?, #dup, #errors_for, #initialize, #match, #message, #negated_errors_for, #negated_match, #negated_message, #negated_type, #type, #with_options

Constructor Details

This class inherits a constructor from Stannum::Constraints::Base

Instance Method Details

#matches?(actual) ⇒ true, false Also known as: match?

Checks that the object is nil or empty.

Returns:

  • (true, false)

    true if the object is nil or empty, otherwise false.

See Also:



33
34
35
36
37
38
39
# File 'lib/stannum/constraints/absence.rb', line 33

def matches?(actual)
  return true if actual.nil?

  return true if actual.respond_to?(:empty?) && actual.empty?

  false
end