Class: Flows::Contract::HashOf
- Inherits:
-
Flows::Contract
- Object
- Flows::Contract
- Flows::Contract::HashOf
- Defined in:
- lib/flows/contract/hash_of.rb
Overview
Contract for Ruby Hash
with specified structure.
Hash can have extra keys. Extra keys will be removed after transform. Underlying contracts' transforms will be applied to correspond values.
Constant Summary collapse
Instance Method Summary collapse
- #check!(other) ⇒ Object
-
#initialize(shape = {}) ⇒ HashOf
constructor
A new instance of HashOf.
- #transform!(other) ⇒ Object
Methods inherited from Flows::Contract
#===, #check, make, #to_proc, #transform
Constructor Details
#initialize(shape = {}) ⇒ HashOf
Returns a new instance of HashOf.
28 29 30 |
# File 'lib/flows/contract/hash_of.rb', line 28 def initialize(shape = {}) @shape = shape.transform_values(&method(:to_contract)) end |
Instance Method Details
#check!(other) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/flows/contract/hash_of.rb', line 32 def check!(other) HASH_CONTRACT.check!(other) errors = check_shape(other) raise Error.new(other, errors.join("\n")) if errors.any? true end |
#transform!(other) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/flows/contract/hash_of.rb', line 42 def transform!(other) check!(other) other .slice(*@shape.keys) .map { |key, value| [key, @shape[key].transform!(value)] } .to_h end |