Class: Stannum::Constraints::Hashes::IndifferentKey

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

Overview

Constraint for validating an indifferent hash key.

To be a valid indifferent Hash key, an object must be a String or a Symbol and cannot be empty.

Examples:

With nil

constraint = Stannum::Constraints::Hashes::IndifferentKey.new
constraint.matches?(nil) #=> false
constraint.errors_for(nil)
#=> [{ type: 'absent', data: {}, path: [], message: nil }]

With an Object

constraint = Stannum::Constraints::Hashes::IndifferentKey.new
constraint.matches?(Object.new.freeze) #=> false
constraint.errors_for(Object.new.freeze)
#=> [{ type: 'is_not_string_or_symbol', data: {}, path: [], message: nil }]

With an empty String

constraint = Stannum::Constraints::Hashes::IndifferentKey.new
constraint.matches?('') #=> false
constraint.errors_for('')
#=> [{ type: 'absent', data: {}, path: [], message: nil }]

With a String

constraint = Stannum::Constraints::Hashes::IndifferentKey.new
constraint.matches?('a string') #=> true

With an empty Symbol

constraint = Stannum::Constraints::Hashes::IndifferentKey.new
constraint.matches?(:'') #=> false
constraint.errors_for(:'')
#=> [{ type: 'absent', data: {}, path: [], message: nil }]

With a Symbol

constraint = Stannum::Constraints::Hashes::IndifferentKey.new
constraint.matches?(:a_symbol) #=> true

Constant Summary collapse

NEGATED_TYPE =

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

'stannum.constraints.hashes.is_string_or_symbol'
TYPE =

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

'stannum.constraints.hashes.is_not_string_or_symbol'

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#==, #clone, #does_not_match?, #dup, #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

#errors_for(actual, errors: nil) ⇒ Stannum::Errors

Note:

This method should only be called for an object that does not match the constraint. Generating errors for a matching object can result in undefined behavior.

Generates an errors object for the given object.

The errors object represents the difference between the given object and the expected properties or behavior. It may be the same for all objects, or different based on the details of the object or the constraint.

Examples:

Generating errors for a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new
errors     = constraint.errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters:

  • actual (Object)

    The object to generate errors for.

  • errors (Stannum::Errors) (defaults to: nil)

    The errors object to append errors to. If an errors object is not given, a new errors object will be created.

Returns:

See Also:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stannum/constraints/hashes/indifferent_key.rb', line 50

def errors_for(actual, errors: nil)
  errors ||= Stannum::Errors.new

  return errors.add(Stannum::Constraints::Presence::TYPE) if actual.nil?

  return super unless indifferent_key_type?(actual)

  return errors unless actual.empty?

  errors.add(Stannum::Constraints::Presence::TYPE)
end

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

Returns true if the object is a non-empty String or Symbol.

Returns:

  • (true, false)

    true if the object is a non-empty String or Symbol.



63
64
65
# File 'lib/stannum/constraints/hashes/indifferent_key.rb', line 63

def matches?(actual)
  indifferent_key_type?(actual) && !actual.empty?
end