Sha256: 2010c52a6bbd8a13f497c52876b506e24d7c2ac312987fe10ee9a434c67f50f1

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Dry
  module Types
    module Builder
      def constrained_type
        Constrained
      end

      def |(other)
        klass = is_a?(Constrained) && other.is_a?(Constrained) ? Sum::Constrained : Sum
        klass.new(self, other)
      end

      def optional
        Types['strict.nil'] | self
      end

      def maybe
        Maybe.new(Types['strict.nil'] | self)
      end

      def constrained(options)
        constrained_type.new(self, rule: Types.Rule(options))
      end

      def default(input = Undefined, &block)
        value = input == Undefined ? block : input

        if value.is_a?(Proc) || valid?(value)
          Default[value].new(self, value: value)
        else
          raise ConstraintError, "default value #{value.inspect} violates constraints"
        end
      end

      def enum(*values)
        Enum.new(constrained(included_in: values), values: values)
      end

      def safe
        Safe.new(self)
      end

      def constructor(constructor = nil, **options, &block)
        Constructor.new(with(options), fn: constructor || block)
      end
    end
  end
end

require 'dry/types/default'
require 'dry/types/constrained'
require 'dry/types/enum'
require 'dry/types/maybe'
require 'dry/types/safe'
require 'dry/types/sum'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-types-0.7.2 lib/dry/types/builder.rb