Module: Dry::Types::Builder
- Included in:
- Constrained, Default, Definition, Optional, Safe, Sum
- Defined in:
- lib/dry/types/builder.rb
Instance Method Summary collapse
- #constrained(options) ⇒ Object
- #constructor(constructor, options = {}) ⇒ Object
- #default(input = nil, &block) ⇒ Object
- #enum(*values) ⇒ Object
- #optional ⇒ Object
- #safe ⇒ Object
- #|(other) ⇒ Object
Instance Method Details
#constrained(options) ⇒ Object
12 13 14 |
# File 'lib/dry/types/builder.rb', line 12 def constrained() Constrained.new(self, rule: Types.Rule()) end |
#constructor(constructor, options = {}) ⇒ Object
34 35 36 |
# File 'lib/dry/types/builder.rb', line 34 def constructor(constructor, = {}) Constructor.new(with(), 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 |
#optional ⇒ Object
8 9 10 |
# File 'lib/dry/types/builder.rb', line 8 def optional Optional.new(Types['nil'] | self) end |
#safe ⇒ Object
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 |