Module: Typed::Builder::BaseType

Included in:
Typed, ArrayType, CoerceType, ConstrainedType, DefaultType, SumType, Struct, Undefined
Defined in:
lib/typed/builder.rb

Instance Method Summary collapse

Instance Method Details

#call(*args) ⇒ Object

Raises:



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.message
end

#constrained(**dry_options, &constraint) ⇒ Object



75
76
77
78
79
# File 'lib/typed/builder.rb', line 75

def constrained(**dry_options, &constraint)
    base = constraint ? ConstrainedType.new(self, &constraint) : self
    base = base.dry_constrained(**dry_options) unless dry_options.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

#missableObject



44
45
46
# File 'lib/typed/builder.rb', line 44

def missable
    Typed.value(Undefined) | self
end

#nullableObject



40
41
42
# File 'lib/typed/builder.rb', line 40

def nullable
    Typed.null | self
end

#process(value) ⇒ Object



88
89
90
# File 'lib/typed/builder.rb', line 88

def process(value)
    Typed::Builder::Result.success(value)
end

#|(other) ⇒ Object



62
63
64
65
66
# File 'lib/typed/builder.rb', line 62

def |(other)
    expected_type other

    SumType.new(self, other)
end