module Typed::Builder::BaseType
Public Instance Methods
call(*args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/typed/builder.rb, line 58 def enum(*values) constrained(included_in: values.map { |value| call(value) }) end
instance(expected_class)
click to toggle source
# File lib/typed/builder.rb, line 54 def instance(expected_class) constrained(type: expected_class) end
missable()
click to toggle source
# File lib/typed/builder.rb, line 44 def missable Typed.value(Undefined) | self end
nullable()
click to toggle source
# File lib/typed/builder.rb, line 40 def nullable Typed.null | self end
process(value)
click to toggle source
# File lib/typed/builder.rb, line 88 def process(value) Typed::Builder::Result.success(value) end
|(other)
click to toggle source
# File lib/typed/builder.rb, line 62 def |(other) expected_type other SumType.new(self, other) end
Protected Instance Methods
dry_constrained(**options)
click to toggle source
# File lib/typed/builder.rb, line 94 def dry_constrained(**options) predicate = ::Dry::Logic::RuleCompiler.new(::Dry::Logic::Predicates).call( options.map { |key, val| ::Dry::Logic::Rule::Predicate.new( ::Dry::Logic::Predicates[:"#{key}?"] ).curry(val).to_ast } ).reduce(:and) constrained do |value| "#{value.inspect} violates #{predicate}" unless predicate.call(value).success? end end
Private Instance Methods
expected_type(type)
click to toggle source
# File lib/typed/builder.rb, line 110 def expected_type(type) raise InvalidType, "Not a Typed type: #{type.inspect}" unless type.is_a?(BaseType) end