Class: Dry::Schema::Key
- Inherits:
-
Object
- Object
- Dry::Schema::Key
- Extended by:
- Core::Cache
- Defined in:
- lib/dry/schema/key.rb
Overview
Key objects used by key maps
Defined Under Namespace
Constant Summary collapse
- DEFAULT_COERCER =
:itself.to_proc.freeze
Instance Attribute Summary collapse
-
#coercer ⇒ Proc, #call
readonly
A key name coercer function.
-
#id ⇒ Symbol
readonly
The key identifier.
-
#name ⇒ Symbol, ...
readonly
The actual key name expected in an input hash.
Class Method Summary collapse
- .[](name, **opts) ⇒ Object private
- .new(*args, **kwargs) ⇒ Object private
Instance Method Summary collapse
- #coercible(&coercer) ⇒ Object private
- #dump ⇒ Object private
-
#initialize(id, name: id, coercer: DEFAULT_COERCER) ⇒ Key
constructor
private
A new instance of Key.
- #new(**new_opts) ⇒ Object private
- #read(source) ⇒ Object private
- #stringified ⇒ Object private
- #to_dot_notation ⇒ Object private
- #write(source, target) ⇒ Object private
Constructor Details
#initialize(id, name: id, coercer: DEFAULT_COERCER) ⇒ Key
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 a new instance of Key.
35 36 37 38 39 |
# File 'lib/dry/schema/key.rb', line 35 def initialize(id, name: id, coercer: DEFAULT_COERCER) @id = id @name = name @coercer = coercer end |
Instance Attribute Details
#coercer ⇒ Proc, #call (readonly)
Returns A key name coercer function.
22 23 24 |
# File 'lib/dry/schema/key.rb', line 22 def coercer @coercer end |
#id ⇒ Symbol (readonly)
Returns The key identifier.
16 17 18 |
# File 'lib/dry/schema/key.rb', line 16 def id @id end |
#name ⇒ Symbol, ... (readonly)
Returns The actual key name expected in an input hash.
19 20 21 |
# File 'lib/dry/schema/key.rb', line 19 def name @name end |
Class Method Details
.[](name, **opts) ⇒ 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.
25 26 27 |
# File 'lib/dry/schema/key.rb', line 25 def self.[](name, **opts) new(name, **opts) end |
.new(*args, **kwargs) ⇒ 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.
30 31 32 |
# File 'lib/dry/schema/key.rb', line 30 def self.new(*args, **kwargs) fetch_or_store(args, kwargs) { super } end |
Instance Method Details
#coercible(&coercer) ⇒ 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.
58 59 60 |
# File 'lib/dry/schema/key.rb', line 58 def coercible(&coercer) new(coercer: coercer) end |
#dump ⇒ 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.
78 79 80 |
# File 'lib/dry/schema/key.rb', line 78 def dump name end |
#new(**new_opts) ⇒ 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.
73 74 75 |
# File 'lib/dry/schema/key.rb', line 73 def new(**new_opts) self.class.new(id, name: name, coercer: coercer, **new_opts) end |
#read(source) ⇒ 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.
42 43 44 45 46 47 48 49 50 |
# File 'lib/dry/schema/key.rb', line 42 def read(source) return unless source.is_a?(::Hash) if source.key?(name) yield(source[name]) elsif source.key?(coerced_name) yield(source[coerced_name]) end end |
#stringified ⇒ 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.
63 64 65 |
# File 'lib/dry/schema/key.rb', line 63 def stringified new(name: name.to_s) 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.
68 69 70 |
# File 'lib/dry/schema/key.rb', line 68 def to_dot_notation [name.to_s] end |
#write(source, target) ⇒ 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.
53 54 55 |
# File 'lib/dry/schema/key.rb', line 53 def write(source, target) read(source) { |value| target[coerced_name] = value } end |