kit-contract edge

Hash class
Kit::Contract::BuiltInContracts::Hash
View Source

Enable Contracts on Hash instances, on keys themselves, on values themselves, and on elements at specific keys.

Supported contract types:

  • of: combo of every_key and every_value as a Hash, more readable for types
  • with: run on the value of specific keys (this is the default when using Hash[data])
  • every: run on every [key, value] pair
  • every_key: run on every key
  • every_value: run on every value
  • instance: run on the hash instance itself
  • size: instance contract about size

Supported internal types of behaviour:

  • every_key: run on every key
  • every_key_value: run on every [key, value]
  • every_value: run on every value
  • keyword_args: run on the value of specific keys
  • instance: run on the hash instance itself

Todo: add exemples.


Link to this section Summary ⚠️ Private APIs are currently hidden.


Link to this section Class methods 9

Link to this method

.every_value(*contracts)

View Source

Convenience methods. They provide a slighly terser external API to instantiate contracts.


Link to this section Instance methods 12

Link to this method

#add_contract(contract, safe: false)

View Source
Link to this method

#every(contracts, safe: false)

View Source

contract Or[Contract, Array.of(Contract)]

Link to this method

#every_key(contracts, safe: false)

View Source

contract Or[Contract, Array.of(Contract)]

Link to this method

#every_value(contracts, safe: false)

View Source

contract Or[Contract, Array.of(Contract)]

Link to this method

#instance(contracts, safe: false)

View Source

contract Or[Contract, Array.of(Contract)]

Link to this method

#of(contracts, safe: false)

View Source

contract Hash.of(Type1 => Type2).size(1)

Link to this method

#setup(keyword_args_contracts = nil)

View Source

contract And[Integer, ->(x) { x > 0 }]

NOTE: this will only be useful when Organizer can handle any signature

Link to this method

#with(contracts, safe: false)

View Source

before Ct::Args[Ct::Hash.of(Ct::NonNil => Ct::Contract)] Add contracts on specific keys.

Link to this method

#without(keys, safe: false)

View Source

before Ct::Args[Ct::Array.of(Ct::NonNil)] Add keys that must not exist on the hash.