Sha256: d94fa4dedc639b930df15e633a6a625e01fb04e4f6fb596f520ec2daac8a19bb
Contents?: true
Size: 1.7 KB
Versions: 3
Compression:
Stored size: 1.7 KB
Contents
require 'dry/data/type/hash' require 'dry/data/type/array' require 'dry/data/type/enum' require 'dry/data/sum_type' require 'dry/data/optional' module Dry module Data class Type attr_reader :constructor attr_reader :primitive class Constrained < Type attr_reader :rule def initialize(constructor, primitive, rule) super(constructor, primitive) @rule = rule end def call(input) result = super(input) if rule.(result).success? result else raise ConstraintError, "#{input.inspect} violates constraints" end end alias_method :[], :call end def self.[](primitive) if primitive == ::Array Type::Array elsif primitive == ::Hash Type::Hash else Type end end def self.strict_constructor(primitive, input) if input.is_a?(primitive) input else raise TypeError, "#{input.inspect} has invalid type" end end def self.passthrough_constructor(input) input end def initialize(constructor, primitive) @constructor = constructor @primitive = primitive end def enum(*values) Enum.new(values, constrained(inclusion: values)) end def optional Optional.new(Data['nil'] | self) end def name primitive.name end def call(input) constructor[input] end alias_method :[], :call def valid?(input) input.is_a?(primitive) end def |(other) SumType.new(self, other) end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
dry-data-0.4.2 | lib/dry/data/type.rb |
dry-data-0.4.1 | lib/dry/data/type.rb |
dry-data-0.4.0 | lib/dry/data/type.rb |