Class: Dry::Types::Map
Overview
Homogeneous mapping. It describes a hash with unknown keys that match a certain type.
Constant Summary
Constants inherited from Nominal
Instance Attribute Summary
Attributes inherited from Nominal
Attributes included from Options
Instance Method Summary collapse
- #call_safe(hash) ⇒ Hash private
- #call_unsafe(hash) ⇒ Hash private
- #constrained? ⇒ Boolean
-
#initialize(primitive, key_type: Types["any"], value_type: Types["any"], meta: EMPTY_HASH) ⇒ Map
constructor
A new instance of Map.
- #key_type ⇒ Type
- #name ⇒ String
-
#to_ast(meta: true) ⇒ Array
An AST representation.
- #try(hash) {|result| ... } ⇒ Result
- #value_type ⇒ Type
Methods inherited from Nominal
[], #default?, #failure, #lax, #optional?, #primitive?, #success, #to_proc, #try_coerce
Methods included from Printable
Methods included from Builder
#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #lax, #maybe, #optional, #|
Methods included from Meta
Methods included from Options
Methods included from Type
Constructor Details
#initialize(primitive, key_type: Types["any"], value_type: Types["any"], meta: EMPTY_HASH) ⇒ Map
Returns a new instance of Map.
26 27 28 |
# File 'lib/dry/types/map.rb', line 26 def initialize(primitive, key_type: Types["any"], value_type: Types["any"], meta: EMPTY_HASH) super(primitive, key_type: key_type, value_type: value_type, meta: ) end |
Instance Method Details
#call_safe(hash) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
67 68 69 |
# File 'lib/dry/types/map.rb', line 67 def call_safe(hash) try(hash) { return yield }.input end |
#call_unsafe(hash) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
56 57 58 59 60 |
# File 'lib/dry/types/map.rb', line 56 def call_unsafe(hash) try(hash) { |failure| raise MapError, failure.error. }.input end |
#constrained? ⇒ Boolean
98 99 100 |
# File 'lib/dry/types/map.rb', line 98 def constrained? value_type.constrained? end |
#name ⇒ String
47 48 49 |
# File 'lib/dry/types/map.rb', line 47 def name "Map" end |
#to_ast(meta: true) ⇒ Array
Returns An AST representation.
88 89 90 91 92 93 |
# File 'lib/dry/types/map.rb', line 88 def to_ast(meta: true) [:map, [key_type.to_ast(meta: true), value_type.to_ast(meta: true), ? self. : EMPTY_HASH]] end |
#try(hash) {|result| ... } ⇒ Result
76 77 78 79 80 81 |
# File 'lib/dry/types/map.rb', line 76 def try(hash) result = coerce(hash) return result if result.success? || !block_given? yield(result) end |