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) ⇒ Object
- #constrained_type ⇒ Object
- #constructor(constructor = nil, **options, &block) ⇒ Object
- #default(input = Undefined, &block) ⇒ Object
- #enum(*values) ⇒ Object
- #maybe ⇒ Object
- #optional ⇒ Object
- #safe ⇒ Object
- #|(other) ⇒ Object
Instance Method Details
#constrained(options) ⇒ Object
21 22 23 |
# File 'lib/dry/types/builder.rb', line 21 def constrained() constrained_type.new(self, rule: Types.Rule()) end |
#constrained_type ⇒ Object
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, **, &block) Constructor.new(with(), 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 |
#maybe ⇒ Object
35 36 37 |
# File 'lib/dry/types/extensions/maybe.rb', line 35 def maybe Maybe.new(Types['strict.nil'] | self) end |
#optional ⇒ Object
17 18 19 |
# File 'lib/dry/types/builder.rb', line 17 def optional Types['strict.nil'] | self end |
#safe ⇒ Object
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 |