Class: Stannum::Constraints::Identity

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

Overview

An Identity constraint checks for the exact object given.

Examples:

Using an Identity constraint

string     = 'Greetings, programs!'
constraint = Stannum::Constraints::Identity.new(string)

constraint.matches?(nil)        #=> false
constraint.matches?('a string') #=> false
constraint.matches?(string.dup) #=> false
constraint.matches?(string)     #=> true

Constant Summary collapse

NEGATED_TYPE =

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

'stannum.constraints.is_value'
TYPE =

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

'stannum.constraints.is_not_value'

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(expected_value, **options) ⇒ Identity

Returns a new instance of Identity.

Parameters:

  • expected_value (Object)

    The expected object.

  • options (Hash<Symbol, Object>)

    Configuration options for the constraint. Defaults to an empty Hash.



26
27
28
29
30
# File 'lib/stannum/constraints/identity.rb', line 26

def initialize(expected_value, **options)
  @expected_value = expected_value

  super(expected_value: expected_value, **options)
end

Instance Attribute Details

#expected_valueObject (readonly)

Returns the expected object.

Returns:

  • (Object)

    the expected object.



33
34
35
# File 'lib/stannum/constraints/identity.rb', line 33

def expected_value
  @expected_value
end

Instance Method Details

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

Checks that the object is the expected value.

Returns:

  • (true, false)

    true if the object is the expected value, otherwise false.

See Also:



41
42
43
# File 'lib/stannum/constraints/identity.rb', line 41

def matches?(actual)
  expected_value.equal?(actual)
end