Class: Flows::Contract::Hash
- Inherits:
-
Flows::Contract
- Object
- Flows::Contract
- Flows::Contract::Hash
- Defined in:
- lib/flows/contract/hash.rb
Overview
Contract for Ruby Hash
with specified contracts for keys and values.
If key contract has transformation - Hash keys will be transformed.
If value contract has transformation - Hash values will be transformed.
Constant Summary collapse
- CHECK_LIMIT =
Stop search for a new type mismatch in keys or values if CHECK_LIMIT errors already found.
Applied separately for keys and values.
10
- HASH_TYPE =
CaseEq.new(::Hash)
Instance Method Summary collapse
- #check!(other) ⇒ Object
-
#initialize(key_contract, value_contract) ⇒ Hash
constructor
A new instance of Hash.
- #transform!(other) ⇒ Object
Methods inherited from Flows::Contract
#===, #check, make, #to_proc, #transform
Constructor Details
#initialize(key_contract, value_contract) ⇒ Hash
Returns a new instance of Hash.
28 29 30 31 |
# File 'lib/flows/contract/hash.rb', line 28 def initialize(key_contract, value_contract) @key_contract = to_contract(key_contract) @value_contract = to_contract(value_contract) end |
Instance Method Details
#check!(other) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flows/contract/hash.rb', line 33 def check!(other) HASH_TYPE.check!(other) unless other.keys.all?(&@key_contract) && other.values.all?(&@value_contract) value_error = report_error(other) raise Error.new(other, value_error) end true end |
#transform!(other) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/flows/contract/hash.rb', line 44 def transform!(other) check!(other) other .transform_keys { |key| @key_contract.transform!(key) } .transform_values { |value| @value_contract.transform!(value) } end |