Class: Typed::Builder::ConstrainedType

Inherits:
Object
  • Object
show all
Includes:
BaseType
Defined in:
lib/typed/builder.rb

Instance Method Summary collapse

Methods included from BaseType

#call, #constrained, #constructor, #default, #enum, #instance, #missable, #nullable, #|

Constructor Details

#initialize(base_type, &constraint) ⇒ ConstrainedType

Returns a new instance of ConstrainedType.



145
146
147
148
# File 'lib/typed/builder.rb', line 145

def initialize(base_type, &constraint)
    @base_type = base_type
    @constraint = constraint
end

Instance Method Details

#process(value) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/typed/builder.rb', line 150

def process(value)
    result = base_type.process(value)
    return result unless result.ok

    error = constraint.call(result.value)
    return result unless error

    Result.failure { error }
end