Class: Dry::Types::Hash
- Defined in:
- lib/dry/types/hash.rb,
lib/dry/types/hash/constructor.rb
Overview
Hash type exposes additional APIs for working with schema hashes
Direct Known Subclasses
Defined Under Namespace
Classes: Constructor
Constant Summary collapse
- NOT_REQUIRED =
{required: false}.freeze
Constants inherited from Nominal
Instance Attribute Summary
Attributes inherited from Nominal
Attributes included from Options
Instance Method Summary collapse
- #constructor_type ⇒ Object private
-
#map(key_type, value_type) ⇒ Map
Build a map type.
- #schema(keys_or_map, meta = EMPTY_HASH) ⇒ Object
-
#to_ast(meta: true) ⇒ Array
An AST representation.
-
#transform_types? ⇒ Boolean
Whether the type transforms types of schemas created by #schema.
- #weak ⇒ Object (also: #permissive, #strict, #strict_with_defaults, #symbolized) private
-
#with_type_transform(proc = nil, &block) ⇒ Hash
Injects a type transformation function for building schemas.
Methods inherited from Nominal
[], #call_safe, #call_unsafe, #coerce, #constrained?, #default?, #failure, #initialize, #lax, #name, #optional?, #primitive?, #success, #to_proc, #try, #try_coerce
Methods included from Printable
Methods included from Builder
#&, #>, #constrained, #constrained_type, #constructor, #default, #enum, #fallback, #lax, #maybe, #optional, #|
Methods included from Meta
#initialize, #meta, #pristine, #with
Methods included from Options
Methods included from Type
Constructor Details
This class inherits a constructor from Dry::Types::Nominal
Instance Method Details
#constructor_type ⇒ Object
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.
77 78 79 |
# File 'lib/dry/types/hash.rb', line 77 def constructor_type ::Dry::Types::Hash::Constructor end |
#map(key_type, value_type) ⇒ Map
Build a map type
40 41 42 43 44 45 46 47 |
# File 'lib/dry/types/hash.rb', line 40 def map(key_type, value_type) Map.new( primitive, key_type: resolve_type(key_type), value_type: resolve_type(value_type), meta: ) end |
#schema(type_map, meta = EMPTY_HASH) ⇒ Dry::Types::Schema #schema(keys) ⇒ Dry::Types::Schema
22 23 24 25 26 27 28 29 30 |
# File 'lib/dry/types/hash.rb', line 22 def schema(keys_or_map, = EMPTY_HASH) if keys_or_map.is_a?(::Array) keys = keys_or_map else keys = build_keys(keys_or_map) end Schema.new(primitive, keys: keys, **, meta: self..merge()) end |
#to_ast(meta: true) ⇒ Array
Returns An AST representation.
95 96 97 |
# File 'lib/dry/types/hash.rb', line 95 def to_ast(meta: true) [:hash, [.slice(:type_transform_fn), ? self. : EMPTY_HASH]] end |
#transform_types? ⇒ Boolean
Whether the type transforms types of schemas created by #schema
86 87 88 |
# File 'lib/dry/types/hash.rb', line 86 def transform_types? ![:type_transform_fn].nil? end |
#weak ⇒ Object Also known as: permissive, strict, strict_with_defaults, symbolized
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.
50 51 52 53 |
# File 'lib/dry/types/hash.rb', line 50 def weak(*) raise "Support for old hash schemas was removed, please refer to the CHANGELOG "\ "on how to proceed with the new API https://github.com/dry-rb/dry-types/blob/main/CHANGELOG.md" end |
#with_type_transform(proc = nil, &block) ⇒ Hash
Injects a type transformation function for building schemas
67 68 69 70 71 72 73 74 |
# File 'lib/dry/types/hash.rb', line 67 def with_type_transform(proc = nil, &block) fn = proc || block raise ArgumentError, "a block or callable argument is required" if fn.nil? handle = Dry::Types::FnContainer.register(fn) with(type_transform_fn: handle) end |