Module: Typed

Extended by:
Builder::BaseType
Defined in:
lib/typed.rb,
lib/typed/struct.rb,
lib/typed/builder.rb,
lib/typed/version.rb

Defined Under Namespace

Modules: Builder, Strict, Undefined Classes: InvalidType, InvalidValue, Struct

Constant Summary collapse

String =
Strict::String.constructor(input: Strict::Int | Strict::Float | Strict::Symbol, &:to_s)
Float =
Strict::Float.constructor(
    input: Strict::String | Strict::Int,
    swallow: [TypeError, ArgumentError]
) { |value| Float(value) }
Int =
Strict::Int
.constructor(
    input: Strict::String,
    swallow: [TypeError, ArgumentError]
) { |value| Integer(value) }
.constructor(
    input: Float,
    swallow: [TypeError, ArgumentError]
) { |value|
    parsed = Integer(value)
    parsed == value ? parsed : value
}
Date =
Strict::Date
.constructor(
    input: String,
    swallow: [TypeError, ArgumentError, RangeError]
) { |value| ::Date.parse(value) }
.constructor(input: Typed.instance(::Time), &:to_date)
Boolean =
Strict::Boolean.constructor(input: String) { |value|
    { 'true' => true, 'false' => false }.fetch(value) { value }
}
Time =
(Strict::DateTime | Strict::Time)
.constructor(input: String, swallow: [TypeError, ArgumentError]) { |value|
    ::ActiveSupport::TimeZone['UTC'].parse(value)
}
.constructor(input: Int | Float, swallow: [TypeError, ArgumentError]) { |value| ::Time.at(value) }
UUID =
String.constrained(format: /\A[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}\z/)
.constructor(input: String, &:downcase)
URL =
String.constrained(format: URI::DEFAULT_PARSER.make_regexp(%w[http https]))
VERSION =
'0.1.1'

Class Method Summary collapse

Methods included from Builder::BaseType

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

Class Method Details

.anyObject



22
23
24
# File 'lib/typed.rb', line 22

def any
    self
end

.array(element_type = Typed.any) ⇒ Object



16
17
18
19
20
# File 'lib/typed.rb', line 16

def array(element_type = Typed.any)
    expected_type(element_type)

    Typed::Builder::ArrayType.new(element_type)
end

.nullObject



26
27
28
# File 'lib/typed.rb', line 26

def null
    value(nil)
end

.value(expected_value) ⇒ Object



30
31
32
# File 'lib/typed.rb', line 30

def value(expected_value)
    constrained(eql: call(expected_value))
end