Class: Dry::Schema::Key

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

Overview

Key objects used by key maps

Direct Known Subclasses

Array, Hash

Defined Under Namespace

Classes: Array, Hash

Constant Summary collapse

DEFAULT_COERCER =
:itself.to_proc.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#coercerProc, #call (readonly)

Returns A key name coercer function.

Returns:

  • (Proc, #call)

    A key name coercer function



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

def coercer
  @coercer
end

#idSymbol (readonly)

Returns The key identifier.

Returns:

  • (Symbol)

    The key identifier



16
17
18
# File 'lib/dry/schema/key.rb', line 16

def id
  @id
end

#nameSymbol, ... (readonly)

Returns The actual key name expected in an input hash.

Returns:

  • (Symbol, String, Object)

    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

#dumpObject

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

#stringifiedObject

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_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.



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