module Dry::Types::Builder
Common API for building types and composition
@api public rubocop:disable Metrics/ModuleLength
Public Instance Methods
Compose two types into an Intersection
type
@param [Type] other
@return [Intersection, Intersection::Constrained]
@api private
# File lib/dry/types/builder.rb, line 44 def &(other) compose(other, Intersection) end
Compose two types into an Implication
type
@param [Type] other
@return [Implication, Implication::Constrained]
@api private
# File lib/dry/types/builder.rb, line 55 def >(other) compose(other, Implication) end
Turn a type into a constrained type
@param [Hash] options constraining rule (see {Types.Rule})
@return [Constrained]
@api public
# File lib/dry/types/builder.rb, line 75 def constrained(options) constrained_type.new(self, rule: Types.Rule(options)) end
@return [Class]
@api private
# File lib/dry/types/builder.rb, line 15 def constrained_type Constrained end
Define a constructor for the type
@param [#call,nil] constructor @param [Hash] options @param [#call,nil] block
@return [Constructor]
@api public
# File lib/dry/types/builder.rb, line 153 def constructor(constructor = nil, **options, &block) constructor_type[with(**options), fn: constructor || block] end
@return [Class]
@api private
# File lib/dry/types/builder.rb, line 22 def constructor_type Constructor end
Turn a type into a type with a default value
@param [Object] input @option [Boolean] shared Whether it’s safe to share the value across type applications @param [#call,nil] block
@raise [ConstraintError]
@return [Default]
@api public
# File lib/dry/types/builder.rb, line 90 def default(input = Undefined, options = EMPTY_HASH, &block) unless input.frozen? || options[:shared] where = Core::Deprecations::STACK.() Core::Deprecations.warn( "#{input.inspect} is mutable."\ " Be careful: types will return the same instance of the default"\ " value every time. Call `.freeze` when setting the default"\ " or pass `shared: true` to discard this warning."\ "\n#{where}", tag: :"dry-types" ) end value = Undefined.default(input, block) type = Default[value].new(self, value) if !type.callable? && !valid?(value) raise ConstraintError.new( "default value #{value.inspect} violates constraints", value ) else type end end
Define an enum on top of the existing type
@param [Array] values
@return [Enum]
@api public
# File lib/dry/types/builder.rb, line 123 def enum(*values) mapping = if values.length == 1 && values[0].is_a?(::Hash) values[0] else values.zip(values).to_h end Enum.new(constrained(included_in: mapping.keys), mapping: mapping) end
Use the given value on type mismatch
@param [Object] value @option [Boolean] shared Whether it’s safe to share the value across type applications @param [#call,nil] fallback
@return [Constructor]
@api public
# File lib/dry/types/builder.rb, line 170 def fallback(value = Undefined, shared: false, &_fallback) # rubocop:disable Metrics/PerceivedComplexity if Undefined.equal?(value) && !block_given? raise ::ArgumentError, "fallback value or a block must be given" end if !block_given? && !valid?(value) raise ConstraintError.new( "fallback value #{value.inspect} violates constraints", value ) end unless value.frozen? || shared where = Core::Deprecations::STACK.() Core::Deprecations.warn( "#{value.inspect} is mutable."\ " Be careful: types will return the same instance of the fallback"\ " value every time. Call `.freeze` when setting the fallback"\ " or pass `shared: true` to discard this warning."\ "\n#{where}", tag: :"dry-types" ) end constructor do |input, type, &_block| type.(input) do |output = input| if block_given? yield(output) else value end end end end
Turn a type into a lax type that will rescue from type-errors and return the original input
@return [Lax]
@api public
# File lib/dry/types/builder.rb, line 140 def lax Lax.new(self) end
Turn a type into a maybe type
@return [Maybe]
@api public
# File lib/dry/types/extensions/maybe.rb, line 99 def maybe Maybe.new(Types["nil"] | self) end
Turn a type into an optional type
@return [Sum]
@api public
# File lib/dry/types/builder.rb, line 64 def optional Types["nil"] | self end
Compose two types into a Sum
type
@param [Type] other
@return [Sum, Sum::Constrained]
@api private
# File lib/dry/types/builder.rb, line 33 def |(other) compose(other, Sum) end
Private Instance Methods
@api private
# File lib/dry/types/builder.rb, line 208 def compose(other, composition_class) klass = if constrained? && other.constrained? composition_class::Constrained else composition_class end klass.new(self, other) end