class Dry::Schema::KeyMap

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.

@api public

Attributes

keys[R]

@return [Array<Key>] A list of defined key objects

Public Class Methods

[](*keys) click to toggle source

Coerce a list of key specs into a key map

@example

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

@return [KeyMap]

@api public

# File lib/dry/schema/key_map.rb, line 37
def self.[](*keys)
  new(keys)
end
new(*args) click to toggle source

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

@param [Array<Symbol>, Array, Hash<Symbol=>Array>] args

@return [KeyMap]

Calls superclass method
# File lib/dry/schema/key_map.rb, line 46
def self.new(*args)
  fetch_or_store(*args) { super }
end
new(keys) click to toggle source

Set key objects

@api private

# File lib/dry/schema/key_map.rb, line 53
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

Public Instance Methods

+(other) click to toggle source

Return a new key map merged with the provided one

@param [KeyMap, Array] other Either a key map or an array with key specs

@return [KeyMap]

# File lib/dry/schema/key_map.rb, line 120
def +(other)
  self.class.new(keys + other.to_a)
end
coercible(&coercer) click to toggle source

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

@return [KeyMap]

@api public

# File lib/dry/schema/key_map.rb, line 88
def coercible(&coercer)
  self.class.new(map { |key| key.coercible(&coercer) })
end
dump() click to toggle source

Dump keys to their spec format

@return [Array]

# File lib/dry/schema/key_map.rb, line 134
def dump
  keys.map(&:dump)
end
each(&block) click to toggle source

Iterate over keys

@api public

# File lib/dry/schema/key_map.rb, line 111
def each(&block)
  keys.each(&block)
end
inspect() click to toggle source

Return a string representation of a key map

@return [String]

# File lib/dry/schema/key_map.rb, line 127
def inspect
  "#<#{self.class}[#{keys.map(&:dump).map(&:inspect).join(", ")}]>"
end
stringified() click to toggle source

Return a new key map with stringified keys

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

@return [KeyMap]

@api public

# File lib/dry/schema/key_map.rb, line 99
def stringified
  self.class.new(map(&:stringified))
end
to_dot_notation() click to toggle source

@api private

# File lib/dry/schema/key_map.rb, line 104
def to_dot_notation
  @to_dot_notation ||= map(&:to_dot_notation).flatten
end
write(source, target = EMPTY_HASH.dup) click to toggle source

Write a new hash based on the source hash

@param [Hash] source The source hash @param [Hash] target The target hash

@return [Hash]

@api public

# File lib/dry/schema/key_map.rb, line 78
def write(source, target = EMPTY_HASH.dup)
  each { |key| key.write(source, target) }
  target
end