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
- #constrained(options) ⇒ Constrained
- #constrained_type ⇒ Constrained
- #constructor(constructor = nil, **options, &block) ⇒ Constructor
- #default(input = Undefined, &block) ⇒ Default
- #enum(*values) ⇒ Enum
- #maybe ⇒ Maybe
- #optional ⇒ Sum
- #safe ⇒ Safe
- #|(other) ⇒ Sum, Sum::Constrained
Instance Method Details
#constrained(options) ⇒ Constrained
25 26 27 |
# File 'lib/dry/types/builder.rb', line 25 def constrained() constrained_type.new(self, rule: Types.Rule()) end |
#constrained_type ⇒ Constrained
7 8 9 |
# File 'lib/dry/types/builder.rb', line 7 def constrained_type Constrained end |
#constructor(constructor = nil, **options, &block) ⇒ Constructor
58 59 60 |
# File 'lib/dry/types/builder.rb', line 58 def constructor(constructor = nil, **, &block) Constructor.new(with(), fn: constructor || block) end |
#default(input = Undefined, &block) ⇒ Default
33 34 35 36 37 38 39 40 41 |
# File 'lib/dry/types/builder.rb', line 33 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) ⇒ Enum
45 46 47 |
# File 'lib/dry/types/builder.rb', line 45 def enum(*values) Enum.new(constrained(included_in: values), values: values) end |
#maybe ⇒ Maybe
44 45 46 |
# File 'lib/dry/types/extensions/maybe.rb', line 44 def maybe Maybe.new(Types['strict.nil'] | self) end |
#optional ⇒ Sum
19 20 21 |
# File 'lib/dry/types/builder.rb', line 19 def optional Types['strict.nil'] | self end |
#|(other) ⇒ Sum, Sum::Constrained
13 14 15 16 |
# File 'lib/dry/types/builder.rb', line 13 def |(other) klass = constrained? && other.constrained? ? Sum::Constrained : Sum klass.new(self, other) end |