Class: Stannum::Constraints::Presence

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

Overview

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

Examples:

Using a Presence constraint

constraint = Stannum::Constraints::Presence.new

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

Using a Presence constraint with an Array

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

Using a Presence constraint with an Hash

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

Constant Summary collapse

NEGATED_TYPE =

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

'stannum.constraints.present'
TYPE =

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

'stannum.constraints.absent'

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 not nil and not empty.

Returns:

  • (true, false)

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

See Also:



33
34
35
36
37
38
39
# 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