Module: Dry::Types::Builder

Included in:
Constrained, Default, Definition, Optional, Safe, Sum
Defined in:
lib/dry/types/builder.rb

Instance Method Summary collapse

Instance Method Details

#constrained(options) ⇒ Object



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

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

#constructor(constructor, options = {}) ⇒ Object



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

def constructor(constructor, options = {})
  Constructor.new(with(options), fn: constructor)
end

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



16
17
18
19
20
21
22
23
24
# File 'lib/dry/types/builder.rb', line 16

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

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

#enum(*values) ⇒ Object



26
27
28
# File 'lib/dry/types/builder.rb', line 26

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

#optionalObject



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

def optional
  Optional.new(Types['nil'] | self)
end

#safeObject



30
31
32
# File 'lib/dry/types/builder.rb', line 30

def safe
  Safe.new(self)
end

#|(other) ⇒ Object



4
5
6
# File 'lib/dry/types/builder.rb', line 4

def |(other)
  Sum.new(self, other)
end