Sha256: bf0337dbc89694b374c66ce4455e3efd6ce0d37cdecbccdb1b8b6c6f689e92f1

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

module Dry
  module Types
    module Builder
      include Dry::Core::Constants

      # @return [Class]
      def constrained_type
        Constrained
      end

      # @param [Type] other
      # @return [Sum, Sum::Constrained]
      def |(other)
        klass = constrained? && other.constrained? ? Sum::Constrained : Sum
        klass.new(self, other)
      end

      # @return [Sum]
      def optional
        Types['strict.nil'] | self
      end

      # @param [Hash] options constraining rule (see {Types.Rule})
      # @return [Constrained]
      def constrained(options)
        constrained_type.new(self, rule: Types.Rule(options))
      end

      # @param [Object] input
      # @param [#call,nil] block
      # @raise [ConstraintError]
      # @return [Default]
      def default(input = Undefined, &block)
        value = input == Undefined ? block : input

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

      # @param [Array] values
      # @return [Enum]
      def enum(*values)
        Enum.new(constrained(included_in: values), values: values)
      end

      # @return [Safe]
      def safe
        Safe.new(self)
      end

      # @param [#call,nil] constructor
      # @param [Hash] options
      # @param [#call,nil] block
      # @return [Constructor]
      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/safe'
require 'dry/types/sum'

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dry-types-0.12.3 lib/dry/types/builder.rb
dry-types-0.12.2 lib/dry/types/builder.rb
dry-types-0.12.1 lib/dry/types/builder.rb
dry-types-0.12.0 lib/dry/types/builder.rb
dry-types-0.11.1 lib/dry/types/builder.rb
dry-types-0.11.0 lib/dry/types/builder.rb
dry-types-0.10.3 lib/dry/types/builder.rb
dry-types-0.10.2 lib/dry/types/builder.rb
dry-types-0.10.1 lib/dry/types/builder.rb
dry-types-0.10.0 lib/dry/types/builder.rb