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)

Instance Attribute Summary collapse

Instance Method Summary collapse

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 Attribute Details

#optionsHash (readonly) Originally defined in module Options

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.

Returns:

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