Class: Stannum::Contracts::IndifferentHashContract

Inherits:
HashContract show all
Defined in:
lib/stannum/contracts/indifferent_hash_contract.rb

Overview

An IndifferentHashContract defines constraints on an hash’s values.

The keys for an IndifferentHashContract must be either strings or symbols. The type of key is ignored when matching - a hash with a string key will match an expected symbol key and vice versa.

Examples:

Creating An Indifferent Hash Contract

hash_contract = Stannum::Contracts::HashContract.new
hash_contract.add_constraint(
  Stannum::Constraints::Presence.new,
  property:      :data,
  property_type: :key,
)

With A Non-Hash Object

hash_contract.matches?(nil) #=> false
errors = hash_contract.errors_for(nil)
#=> [{ type: 'is_not_type', data: { type: Hash }, path: [], message: nil }]

With An Empty Hash

hash_contract.matches?({}) #=> false
errors = hash_contract.errors_for({})
#=> [{ type: 'absent', data: {}, path: [:data], message: nil }]

With A Hash With String Keys

hash = { 'data' => {} }
hash_contract.matches?(hash)   #=> true
hash_contract.errors_for(hash) #=> []

With A Hash With Symbol Keys

hash = { data: {} }
hash_contract.matches?(hash)   #=> true
hash_contract.errors_for(hash) #=> []

Direct Known Subclasses

Parameters::KeywordsContract

Constant Summary

Constants inherited from Stannum::Constraints::Base

Stannum::Constraints::Base::NEGATED_TYPE, Stannum::Constraints::Base::TYPE

Instance Attribute Summary

Attributes inherited from Stannum::Constraints::Base

#options

Instance Method Summary collapse

Methods inherited from HashContract

#key_type, #value_type

Methods inherited from MapContract

#add_key_constraint, #allow_extra_keys?, #expected_keys, #with_options

Methods inherited from Stannum::Contract

#add_constraint, #add_property_constraint

Methods inherited from Base

#==, #add_constraint, #concat, #does_not_match?, #each_constraint, #each_pair, #errors_for, #match, #matches?, #negated_errors_for, #negated_match

Methods inherited from Stannum::Constraints::Base

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

Constructor Details

#initialize(allow_extra_keys: false, value_type: nil, **options, &block) ⇒ IndifferentHashContract

Returns a new instance of IndifferentHashContract.

Parameters:

  • allow_extra_keys (true, false) (defaults to: false)

    If true, the contract will match hashes with keys that are not constrained by the contract.

  • value_type (Stannum::Constraints::Base, Class, nil) (defaults to: nil)

    If set, then the constraint will check the types of each value in the Hash against the expected type and will fail if any values do not match.

  • options (Hash<Symbol, Object>)

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stannum/contracts/indifferent_hash_contract.rb', line 48

def initialize(
  allow_extra_keys: false,
  value_type:       nil,
  **options,
  &block
)
  super(
    allow_extra_keys: allow_extra_keys,
    key_type:         Stannum::Constraints::Hashes::IndifferentKey.new,
    value_type:       value_type,
    **options,
    &block
  )
end