Class: Dry::Schema::KeyMap

Inherits:
Object
  • Object
show all
Extended by:
Core::Cache
Includes:
Enumerable
Defined in:
lib/dry/schema/key_map.rb

Overview

Represents a list of keys defined by the DSL

KeyMap objects expose an API for introspecting schema keys and the ability to rebuild an input hash using configured coercer function.

Instances of this class are used as the very first step by the schema processors.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys) ⇒ KeyMap

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.

Set key objects



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dry/schema/key_map.rb', line 50

def initialize(keys)
  @keys = keys.map { |key|
    case key
    when Hash
      root, rest = key.flatten
      Key::Hash[root, members: KeyMap[*rest]]
    when Array
      root, rest = key
      Key::Array[root, member: KeyMap[*rest]]
    when Key
      key
    else
      Key[key]
    end
  }
end

Instance Attribute Details

#keysArray<Key> (readonly)

Returns A list of defined key objects.

Returns:

  • (Array<Key>)

    A list of defined key objects



22
23
24
# File 'lib/dry/schema/key_map.rb', line 22

def keys
  @keys
end

Class Method Details

.[](*keys) ⇒ KeyMap

Coerce a list of key specs into a key map

Examples:

KeyMap[:id, :name]
KeyMap[:title, :artist, tags: [:name]]
KeyMap[:title, :artist, [:tags]]

Returns:



34
35
36
# File 'lib/dry/schema/key_map.rb', line 34

def self.[](*keys)
  new(keys)
end

.new(*args) ⇒ KeyMap

Build new, or returned a cached instance of a key map

Parameters:

  • args (Array<Symbol>, Array, Hash<Symbol=>Array>)

Returns:



43
44
45
# File 'lib/dry/schema/key_map.rb', line 43

def self.new(*args)
  fetch_or_store(*args) { super }
end

Instance Method Details

#+(other) ⇒ KeyMap

Return a new key map merged with the provided one

Parameters:

  • other (KeyMap, Array)

    Either a key map or an array with key specs

Returns:



117
118
119
# File 'lib/dry/schema/key_map.rb', line 117

def +(other)
  self.class.new(keys + other.to_a)
end

#coercible(&coercer) ⇒ KeyMap

Return a new key map that is configured to coerce keys using provided coercer function

Returns:



85
86
87
# File 'lib/dry/schema/key_map.rb', line 85

def coercible(&coercer)
  self.class.new(map { |key| key.coercible(&coercer) })
end

#dumpArray

Dump keys to their spec format

Returns:

  • (Array)


131
132
133
# File 'lib/dry/schema/key_map.rb', line 131

def dump
  keys.map(&:dump)
end

#each(&block) ⇒ Object

Iterate over keys



108
109
110
# File 'lib/dry/schema/key_map.rb', line 108

def each(&block)
  keys.each(&block)
end

#inspectString

Return a string representation of a key map

Returns:

  • (String)


124
125
126
# File 'lib/dry/schema/key_map.rb', line 124

def inspect
  "#<#{self.class}[#{keys.map(&:dump).map(&:inspect).join(", ")}]>"
end

#stringifiedKeyMap

Return a new key map with stringified keys

A stringified key map is suitable for reading hashes with string keys

Returns:



96
97
98
# File 'lib/dry/schema/key_map.rb', line 96

def stringified
  self.class.new(map(&:stringified))
end

#to_dot_notationObject

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.



101
102
103
# File 'lib/dry/schema/key_map.rb', line 101

def to_dot_notation
  @to_dot_notation ||= map(&:to_dot_notation).flatten
end

#write(source, target = EMPTY_HASH.dup) ⇒ Hash

Write a new hash based on the source hash

Parameters:

  • source (Hash)

    The source hash

  • target (Hash) (defaults to: EMPTY_HASH.dup)

    The target hash

Returns:

  • (Hash)


75
76
77
78
# File 'lib/dry/schema/key_map.rb', line 75

def write(source, target = EMPTY_HASH.dup)
  each { |key| key.write(source, target) }
  target
end