Class: Dry::Schema::KeyMap
- Inherits:
-
Object
- Object
- Dry::Schema::KeyMap
- 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
-
#keys ⇒ Array<Key>
readonly
A list of defined key objects.
Class Method Summary collapse
-
.[](*keys) ⇒ KeyMap
Coerce a list of key specs into a key map.
-
.new(*args) ⇒ KeyMap
Build new, or returned a cached instance of a key map.
Instance Method Summary collapse
-
#+(other) ⇒ KeyMap
Return a new key map merged with the provided one.
-
#coercible(&coercer) ⇒ KeyMap
Return a new key map that is configured to coerce keys using provided coercer function.
-
#dump ⇒ Array
Dump keys to their spec format.
-
#each(&block) ⇒ Object
Iterate over keys.
-
#initialize(keys) ⇒ KeyMap
constructor
private
Set key objects.
-
#inspect ⇒ String
Return a string representation of a key map.
-
#stringified ⇒ KeyMap
Return a new key map with stringified keys.
- #to_dot_notation ⇒ Object private
-
#write(source, target = EMPTY_HASH.dup) ⇒ Hash
Write a new hash based on the source hash.
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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dry/schema/key_map.rb', line 51 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
#keys ⇒ Array<Key> (readonly)
Returns A list of defined key objects.
23 24 25 |
# File 'lib/dry/schema/key_map.rb', line 23 def keys @keys end |
Class Method Details
.[](*keys) ⇒ KeyMap
Coerce a list of key specs into a key map
35 36 37 |
# File 'lib/dry/schema/key_map.rb', line 35 def self.[](*keys) new(keys) end |
.new(*args) ⇒ KeyMap
Build new, or returned a cached instance of a key map
44 45 46 |
# File 'lib/dry/schema/key_map.rb', line 44 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
118 119 120 |
# File 'lib/dry/schema/key_map.rb', line 118 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
86 87 88 |
# File 'lib/dry/schema/key_map.rb', line 86 def coercible(&coercer) self.class.new(map { |key| key.coercible(&coercer) }) end |
#dump ⇒ Array
Dump keys to their spec format
132 133 134 |
# File 'lib/dry/schema/key_map.rb', line 132 def dump keys.map(&:dump) end |
#each(&block) ⇒ Object
Iterate over keys
109 110 111 |
# File 'lib/dry/schema/key_map.rb', line 109 def each(&block) keys.each(&block) end |
#inspect ⇒ String
Return a string representation of a key map
125 126 127 |
# File 'lib/dry/schema/key_map.rb', line 125 def inspect "#<#{self.class}[#{keys.map(&:dump).map(&:inspect).join(", ")}]>" end |
#stringified ⇒ KeyMap
Return a new key map with stringified keys
A stringified key map is suitable for reading hashes with string keys
97 98 99 |
# File 'lib/dry/schema/key_map.rb', line 97 def stringified self.class.new(map(&:stringified)) end |
#to_dot_notation ⇒ Object
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.
102 103 104 |
# File 'lib/dry/schema/key_map.rb', line 102 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
76 77 78 79 |
# File 'lib/dry/schema/key_map.rb', line 76 def write(source, target = EMPTY_HASH.dup) each { |key| key.write(source, target) } target end |