Class: Dry::Types::Map

Inherits:
Nominal show all
Defined in:
lib/dry/types/map.rb

Overview

Homogeneous mapping. It describes a hash with unknown keys that match a certain type.

Examples:

type = Dry::Types['hash'].map(
  Dry::Types['integer'].constrained(gteq: 1, lteq: 10),
  Dry::Types['string']
)

type.(1 => 'right')
# => {1 => 'right'}

type.('1' => 'wrong')
# Dry::Types::MapError: "1" violates constraints (type?(Integer, "1")
#                                                 AND gteq?(1, "1")
#                                                 AND lteq?(10, "1") failed)

type.(11 => 'wrong')
# Dry::Types::MapError: 11 violates constraints (lteq?(10, 11) failed)

Constant Summary

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

[], #default?, #failure, #lax, #optional?, #primitive?, #success, #to_proc, #try_coerce

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained, #constrained_type, #constructor, #constructor_type, #default, #enum, #fallback, #lax, #maybe, #optional, #|

Methods included from Meta

#meta, #pristine, #with

Methods included from Options

#with

Methods included from Type

#call, #valid?

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: 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.

Parameters:

Returns:



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.

Parameters:

Returns:



56
57
58
59
60
# File 'lib/dry/types/map.rb', line 56

def call_unsafe(hash)
  try(hash) { |failure|
    raise MapError, failure.error.message
  }.input
end

#constrained?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/dry/types/map.rb', line 98

def constrained?
  value_type.constrained?
end

#key_typeType

Returns:



33
34
35
# File 'lib/dry/types/map.rb', line 33

def key_type
  options[:key_type]
end

#nameString

Returns:

  • (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.

Parameters:

  • meta (Boolean) (defaults to: true)

    Whether to dump the meta to the AST

Returns:

  • (Array)

    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),
    meta ? self.meta : EMPTY_HASH]]
end

#try(hash) {|result| ... } ⇒ Result

Parameters:

Yields:

  • (result)

Returns:



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

#value_typeType

Returns:



40
41
42
# File 'lib/dry/types/map.rb', line 40

def value_type
  options[:value_type]
end