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 ⇒ Object
readonly
@!attribute id @return [Proc, #call] A key name coercer function @api public.
-
#id ⇒ Object
readonly
@!attribute id @return [Symbol] The key identifier @api public.
-
#name ⇒ Object
readonly
@!attribute name @return [Symbol, String, Object] The actual key name expected in an input hash @api public.
Class Method Summary collapse
- .[](name, **opts) ⇒ Object private
- .new(*args) ⇒ 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 = EMPTY_HASH) ⇒ Object private
- #read(source) ⇒ Object private
- #stringified ⇒ 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.
41 42 43 44 45 |
# File 'lib/dry/schema/key.rb', line 41 def initialize(id, name: id, coercer: DEFAULT_COERCER) @id = id @name = name @coercer = coercer end |
Instance Attribute Details
#coercer ⇒ Object (readonly)
@!attribute id
@return [Proc, #call] A key name coercer function
@api public
28 29 30 |
# File 'lib/dry/schema/key.rb', line 28 def coercer @coercer end |
#id ⇒ Object (readonly)
@!attribute id
@return [Symbol] The key identifier
@api public
18 19 20 |
# File 'lib/dry/schema/key.rb', line 18 def id @id end |
#name ⇒ Object (readonly)
@!attribute name
@return [Symbol, String, Object] The actual key name expected in an input hash
@api public
23 24 25 |
# File 'lib/dry/schema/key.rb', line 23 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.
31 32 33 |
# File 'lib/dry/schema/key.rb', line 31 def self.[](name, **opts) new(name, **opts) end |
.new(*args) ⇒ 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.
36 37 38 |
# File 'lib/dry/schema/key.rb', line 36 def self.new(*args) fetch_or_store(*args) { 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.
62 63 64 |
# File 'lib/dry/schema/key.rb', line 62 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.
77 78 79 |
# File 'lib/dry/schema/key.rb', line 77 def dump name end |
#new(new_opts = EMPTY_HASH) ⇒ 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.
72 73 74 |
# File 'lib/dry/schema/key.rb', line 72 def new(new_opts = EMPTY_HASH) self.class.new(id, { name: name, coercer: coercer }.merge(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.
48 49 50 51 52 53 54 |
# File 'lib/dry/schema/key.rb', line 48 def read(source) 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.
67 68 69 |
# File 'lib/dry/schema/key.rb', line 67 def stringified new(name: 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.
57 58 59 |
# File 'lib/dry/schema/key.rb', line 57 def write(source, target) read(source) { |value| target[coerced_name] = value } end |