class Dry::Types::Schema::Key
@api private
Proxy type for schema keys. Contains only key name and whether it’s required or not. All other calls deletaged to the wrapped type.
@see Dry::Types::Schema
Attributes
name[R]
@return [Symbol]
Public Class Methods
new(type, name, required: Undefined, **options)
click to toggle source
@api private
Calls superclass method
Dry::Types::Decorator::new
# File lib/dry/types/schema/key.rb, line 26 def initialize(type, name, required: Undefined, **options) required = Undefined.default(required) do type.meta.fetch(:required) { !type.meta.fetch(:omittable, false) } end unless name.is_a?(::Symbol) raise ArgumentError, "Schemas can only contain symbol keys, #{name.inspect} given" end super(type, name, required: required, **options) @name = name end
Public Instance Methods
call_safe(input, &block)
click to toggle source
@api private
# File lib/dry/types/schema/key.rb, line 40 def call_safe(input, &block) type.call_safe(input, &block) end
call_unsafe(input)
click to toggle source
@api private
# File lib/dry/types/schema/key.rb, line 45 def call_unsafe(input) type.call_unsafe(input) end
lax()
click to toggle source
Turn key into a lax type. Lax
types are not strict hence such keys are not required
@return [Lax]
@api public
# File lib/dry/types/schema/key.rb, line 99 def lax __new__(type.lax).required(false) end
maybe()
click to toggle source
@api private
# File lib/dry/types/extensions/maybe.rb, line 107 def maybe __new__(type.maybe) end
meta(data = Undefined)
click to toggle source
@api public
Calls superclass method
# File lib/dry/types/schema/key.rb, line 131 def meta(data = Undefined) if Undefined.equal?(data) || !data.key?(:omittable) super else self.class.warn( "Using meta for making schema keys is deprecated, " \ "please use .omittable or .required(false) instead" \ "\n" + Core::Deprecations::STACK.() ) super.required(!data[:omittable]) end end
omittable()
click to toggle source
Make key not required
@return [Dry::Types::Schema::Key]
@api public
# File lib/dry/types/schema/key.rb, line 90 def omittable required(false) end
optional()
click to toggle source
Make wrapped type optional
@return [Key]
@api public
# File lib/dry/types/schema/key.rb, line 108 def optional __new__(type.optional) end
required(required = Undefined)
click to toggle source
Control whether the key is required
@overload required
@return [Boolean]
@overload required(required)
Change key's "requireness" @param [Boolean] required New value @return [Dry::Types::Schema::Key]
@api public
# File lib/dry/types/schema/key.rb, line 77 def required(required = Undefined) if Undefined.equal?(required) options.fetch(:required) else with(required: required) end end
required?()
click to toggle source
Whether the key is required in schema input
@return [Boolean]
@api public
# File lib/dry/types/schema/key.rb, line 61 def required? options.fetch(:required) end
to_ast(meta: true)
click to toggle source
Dump to internal AST representation
@return [Array]
@api public
# File lib/dry/types/schema/key.rb, line 117 def to_ast(meta: true) [ :key, [ name, required, type.to_ast(meta: meta) ] ] end
try(input, &block)
click to toggle source
@api public
# File lib/dry/types/schema/key.rb, line 52 def try(input, &block) type.try(input, &block) end
Private Instance Methods
decorate?(response)
click to toggle source
@api private
# File lib/dry/types/schema/key.rb, line 147 def decorate?(response) response.is_a?(Type) end