class Stannum::Constraints::Presence

A presence constraint asserts that the object is not nil and not empty.

@example Using a Presence constraint

constraint = Stannum::Constraints::Presence.new

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

@example Using a Presence constraint with an Array

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

@example Using a Presence constraint with an Hash

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

Constants

NEGATED_TYPE

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

TYPE

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

Public Instance Methods

match?(actual)
Alias for: matches?
matches?(actual) click to toggle source

Checks that the object is not nil and not empty.

@return [true, false] false if the object is nil or empty, otherwise true.

@see Stannum::Constraint#matches?

# File lib/stannum/constraints/presence.rb, line 33
def matches?(actual)
  return false if actual.nil?

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

  true
end
Also aliased as: match?