class Dry::Schema::Key

Key objects used by key maps

@api public

Constants

DEFAULT_COERCER

Attributes

coercer[R]

@return [Proc, call] A key name coercer function

id[R]

@return [Symbol] The key identifier

name[R]

@return [Symbol, String, Object] The actual key name expected in an input hash

Public Class Methods

[](name, **opts) click to toggle source

@api private

# File lib/dry/schema/key.rb, line 25
def self.[](name, **opts)
  new(name, **opts)
end
new(*args, **kwargs) click to toggle source

@api private

Calls superclass method
# File lib/dry/schema/key.rb, line 30
def self.new(*args, **kwargs)
  fetch_or_store(args, kwargs) { super }
end
new(id, name: id, coercer: DEFAULT_COERCER) click to toggle source

@api private

# File lib/dry/schema/key.rb, line 35
def initialize(id, name: id, coercer: DEFAULT_COERCER)
  @id = id
  @name = name
  @coercer = coercer
end

Public Instance Methods

coercible(&coercer) click to toggle source

@api private

# File lib/dry/schema/key.rb, line 58
def coercible(&coercer)
  new(coercer: coercer)
end
dump() click to toggle source

@api private

# File lib/dry/schema/key.rb, line 78
def dump
  name
end
new(**new_opts) click to toggle source

@api private

# 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) { |source| ... } click to toggle source

@api private

# 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() click to toggle source

@api private

# File lib/dry/schema/key.rb, line 63
def stringified
  new(name: name.to_s)
end
to_dot_notation() click to toggle source

@api private

# File lib/dry/schema/key.rb, line 68
def to_dot_notation
  [name.to_s]
end
write(source, target) click to toggle source

@api private

# File lib/dry/schema/key.rb, line 53
def write(source, target)
  read(source) { |value| target[coerced_name] = value }
end

Private Instance Methods

coerced_name() click to toggle source

@api private

# File lib/dry/schema/key.rb, line 85
def coerced_name
  @__coerced_name__ ||= coercer[name]
end