Class: Highway::Steps::Types::Hash
- Defined in:
- lib/highway/steps/types/hash.rb
Overview
This class represents a dictionary parameter type.
Instance Method Summary collapse
-
#initialize(element_type, validate: nil) ⇒ Hash
constructor
Initialize an instance.
-
#typecheck(value) ⇒ Hash?
Typecheck and coerce a value if possible.
Methods inherited from Any
#typecheck_and_validate, #validate
Constructor Details
#initialize(element_type, validate: nil) ⇒ Hash
Initialize an instance.
22 23 24 25 |
# File 'lib/highway/steps/types/hash.rb', line 22 def initialize(element_type, validate: nil) super(validate: validate) @element_type = element_type end |
Instance Method Details
#typecheck(value) ⇒ Hash?
Typecheck and coerce a value if possible.
This method returns a typechecked and coerced value or ‘nil` if value has invalid type and can’t be coerced.
35 36 37 38 39 |
# File 'lib/highway/steps/types/hash.rb', line 35 def typecheck(value) return nil unless value.is_a?(::Hash) typechecked = Utilities::hash_map(value) { |key, element| [key, @element_type.typecheck_and_validate(element)] } typechecked if typechecked.values.all? { |element| !element.nil? } end |