Module: Typed::Builder::BaseType
- Included in:
- Typed, ArrayType, CoerceType, ConstrainedType, DefaultType, SumType, Struct, Undefined
- Defined in:
- lib/typed/builder.rb
Instance Method Summary collapse
- #call(*args) ⇒ Object
- #constrained(**dry_options, &constraint) ⇒ Object
- #constructor(input: Typed.any, swallow: [], &block) ⇒ Object
- #default(new_value = Typed::Undefined, &block) ⇒ Object
- #enum(*values) ⇒ Object
- #instance(expected_class) ⇒ Object
- #missable ⇒ Object
- #nullable ⇒ Object
- #process(value) ⇒ Object
- #|(other) ⇒ Object
Instance Method Details
#call(*args) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/typed/builder.rb', line 81 def call(*args) result = process((args + [Typed::Undefined]).first) return result.value if result.ok raise InvalidValue, result. end |
#constrained(**dry_options, &constraint) ⇒ Object
75 76 77 78 79 |
# File 'lib/typed/builder.rb', line 75 def constrained(**, &constraint) base = constraint ? ConstrainedType.new(self, &constraint) : self base = base.dry_constrained(**) unless .empty? base end |
#constructor(input: Typed.any, swallow: [], &block) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/typed/builder.rb', line 68 def constructor(input: Typed.any, swallow: [], &block) expected_type(input) return self unless block_given? CoerceType.new(input, self, swallow, &block) end |
#default(new_value = Typed::Undefined, &block) ⇒ Object
48 49 50 51 52 |
# File 'lib/typed/builder.rb', line 48 def default(new_value = Typed::Undefined, &block) call(new_value) unless block block ||= -> { new_value } DefaultType.new(self) { call(block.call) } end |
#enum(*values) ⇒ Object
58 59 60 |
# File 'lib/typed/builder.rb', line 58 def enum(*values) constrained(included_in: values.map { |value| call(value) }) end |
#instance(expected_class) ⇒ Object
54 55 56 |
# File 'lib/typed/builder.rb', line 54 def instance(expected_class) constrained(type: expected_class) end |
#missable ⇒ Object
44 45 46 |
# File 'lib/typed/builder.rb', line 44 def missable Typed.value(Undefined) | self end |
#nullable ⇒ Object
40 41 42 |
# File 'lib/typed/builder.rb', line 40 def nullable Typed.null | self end |