Class: Dry::Types::Hash

Inherits:
Nominal show all
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

Schema

Defined Under Namespace

Classes: Constructor

Constant Summary collapse

NOT_REQUIRED =
{required: false}.freeze

Constants inherited from Nominal

Nominal::ALWAYS

Instance Attribute Summary

Attributes inherited from Nominal

#primitive

Attributes included from Options

#options

Instance Method Summary collapse

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

#to_s

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

#initialize, #with

Methods included from Type

#call, #valid?

Constructor Details

This class inherits a constructor from Dry::Types::Nominal

Instance Method Details

#constructor_typeObject

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

Parameters:

Returns:



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: meta
  )
end

#schema(type_map, meta = EMPTY_HASH) ⇒ Dry::Types::Schema #schema(keys) ⇒ Dry::Types::Schema

Overloads:



22
23
24
25
26
27
28
29
30
# File 'lib/dry/types/hash.rb', line 22

def schema(keys_or_map, meta = 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, **options, meta: self.meta.merge(meta))
end

#to_ast(meta: true) ⇒ Array

Returns An AST representation.

Parameters:

  • meta (Boolean) (defaults to: true)

    Whether to dump the meta to the AST

Returns:

  • (Array)

    An AST representation



95
96
97
# File 'lib/dry/types/hash.rb', line 95

def to_ast(meta: true)
  [:hash, [options.slice(:type_transform_fn), meta ? self.meta : EMPTY_HASH]]
end

#transform_types?Boolean

Whether the type transforms types of schemas created by #schema

Returns:

  • (Boolean)


86
87
88
# File 'lib/dry/types/hash.rb', line 86

def transform_types?
  !options[:type_transform_fn].nil?
end

#weakObject 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

Parameters:

  • proc (#call, nil) (defaults to: nil)
  • block (#call, nil)

Returns:

Raises:

  • (ArgumentError)


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