Module: Dry::Types::Builder

Includes:
Core::Constants
Included in:
Constrained, Default, Definition, Maybe, Safe, Sum
Defined in:
lib/dry/types/builder.rb,
lib/dry/types/extensions/maybe.rb

Instance Method Summary collapse

Instance Method Details

#constrained(options) ⇒ Object



21
22
23
# File 'lib/dry/types/builder.rb', line 21

def constrained(options)
  constrained_type.new(self, rule: Types.Rule(options))
end

#constrained_typeObject



8
9
10
# File 'lib/dry/types/builder.rb', line 8

def constrained_type
  Constrained
end

#constructor(constructor = nil, **options, &block) ⇒ Object



43
44
45
# File 'lib/dry/types/builder.rb', line 43

def constructor(constructor = nil, **options, &block)
  Constructor.new(with(options), fn: constructor || block)
end

#default(input = Undefined, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/dry/types/builder.rb', line 25

def default(input = Undefined, &block)
  value = input == Undefined ? block : input

  if value.is_a?(Proc) || valid?(value)
    Default[value].new(self, value)
  else
    raise ConstraintError.new("default value #{value.inspect} violates constraints", value)
  end
end

#enum(*values) ⇒ Object



35
36
37
# File 'lib/dry/types/builder.rb', line 35

def enum(*values)
  Enum.new(constrained(included_in: values), values: values)
end

#maybeObject



35
36
37
# File 'lib/dry/types/extensions/maybe.rb', line 35

def maybe
  Maybe.new(Types['strict.nil'] | self)
end

#optionalObject



17
18
19
# File 'lib/dry/types/builder.rb', line 17

def optional
  Types['strict.nil'] | self
end

#safeObject



39
40
41
# File 'lib/dry/types/builder.rb', line 39

def safe
  Safe.new(self)
end

#|(other) ⇒ Object



12
13
14
15
# File 'lib/dry/types/builder.rb', line 12

def |(other)
  klass = constrained? && other.constrained? ? Sum::Constrained : Sum
  klass.new(self, other)
end