Class: Stannum::Contracts::HashContract

Inherits:
MapContract show all
Defined in:
lib/stannum/contracts/hash_contract.rb

Overview

A HashContract defines constraints on an hash’s values.

Examples:

Creating A Hash Contract

hash_contract = Stannum::Contracts::HashContract.new

hash_contract.add_constraint(
  negated_type:  'example.is_boolean',
  property:      :ok,
  property_type: :key,
  type:          'example.is_not_boolean'
) { |actual| actual == true || actual == false }
hash_contract.add_constraint(
  Stannum::Constraints::Type.new(Hash),
  property:      :data,
  property_type: :key,
)
hash_contract.add_constraint(
  Stannum::Constraints::Presence.new,
  property:      :signature,
  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 }]

hash_contract.does_not_match?(nil)          #=> true
hash_contract.negated_errors_for?(nil).to_a #=> []

With A Hash That Matches None Of The Key Constraints

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

hash_contract.does_not_match?({}) #=> false
errors.to_a
#=> [
  { type: 'is_type', data: { type: Hash }, path: [], message: nil }
]

With A Hash That Matches Some Of The Key Constraints

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

hash_contract.does_not_match?(hash) #=> false
errors = hash_contract.negated_errors_for?(hash)
errors.to_a
#=> [
  { type: 'is_type', data: { type: Hash }, path: [], message: nil },
  { type: 'is_boolean', data: {}, path: [:ok], message: nil }
]

With A Hash That Matches All Of The Key Constraints

hash = { ok: true, data: {}, signature: 'abc' }
hash_contract.matches?(hash)        #=> true
hash_contract.errors_for(hash).to_a #=> []

hash_contract.does_not_match?(hash) #=> false
errors = hash_contract.negated_errors_for?(hash)
errors.to_a
#=> [
  { type: 'is_type', data: { type: Hash }, path: [], message: nil },
  { type: 'is_boolean', data: {}, path: [:ok], message: nil },
  { type: 'present', data: {}, path: [:signature], message: nil },
]

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 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, key_type: nil, value_type: nil, **options, &block) ⇒ HashContract

Returns a new instance of HashContract.

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.

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

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

  • 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.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stannum/contracts/hash_contract.rb', line 96

def initialize(
  allow_extra_keys: false,
  key_type:         nil,
  value_type:       nil,
  **options,
  &block
)
  super(
    allow_extra_keys: allow_extra_keys,
    key_type:         key_type,
    value_type:       value_type,
    **options,
    &block
  )
end

Instance Method Details

#key_typeStannum::Constraints::Base, ...

Returns the expected type for the keys in the Hash, if any.

Returns:



114
115
116
# File 'lib/stannum/contracts/hash_contract.rb', line 114

def key_type
  options[:key_type]
end

#value_typeStannum::Constraints::Base, ...

Returns the expected type for the values in the Hash, if any.

Returns:



120
121
122
# File 'lib/stannum/contracts/hash_contract.rb', line 120

def value_type
  options[:value_type]
end