Class: Stannum::Constraints::Equality

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

Overview

An Equality constraint uses #== to compare the actual and expected objects.

Examples:

Using an Equality constraint

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

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

Constant Summary collapse

NEGATED_TYPE =

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

'stannum.constraints.is_equal_to'
TYPE =

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

'stannum.constraints.is_not_equal_to'

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) ⇒ Equality

Returns a new instance of Equality.

Parameters:

  • expected_value (Object)

    The expected object.

  • options (Hash<Symbol, Object>)

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



27
28
29
30
31
# File 'lib/stannum/constraints/equality.rb', line 27

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.



34
35
36
# File 'lib/stannum/constraints/equality.rb', line 34

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:



42
43
44
# File 'lib/stannum/constraints/equality.rb', line 42

def matches?(actual)
  expected_value == actual
end