module Dry::Types::BuilderMethods

Common API for building type objects in a convenient way

@api public

Public Instance Methods

Array(type) click to toggle source

Build an array type.

Shortcut for Array#of.

@example

Types::Strings = Types.Array(Types::String)

@param [Dry::Types::Type] type

@return [Dry::Types::Array]

# File lib/dry/types/builder_methods.rb, line 26
def Array(type)
  Strict(::Array).of(type)
end
Constant(object) click to toggle source

Build a type with a single value The equality check done with `equal?`

@param [Object] object

@return [Dry::Types::Type]

# File lib/dry/types/builder_methods.rb, line 71
def Constant(object)
  Nominal(object.class).constrained(is: object)
end
Constructor(klass, cons = nil, &block) click to toggle source

Build a constructor type If no constructor block given it uses .new method

@param [Class] klass @param [#call,nil] cons Value constructor @param [#call,nil] block Value constructor

@return [Dry::Types::Type]

# File lib/dry/types/builder_methods.rb, line 83
def Constructor(klass, cons = nil, &block)
  if klass.is_a?(Type)
    if cons || block
      klass.constructor(cons || block)
    else
      klass
    end
  else
    Nominal(klass).constructor(cons || block || klass.method(:new))
  end
end
Hash(type_map) click to toggle source

Build a hash schema

@param [Hash{Symbol => Dry::Types::Type}] type_map

@return [Dry::Types::Array]

# File lib/dry/types/builder_methods.rb, line 35
def Hash(type_map)
  Strict(::Hash).schema(type_map)
end
Instance(klass) click to toggle source

Build a type which values are instances of a given class Values are checked using `is_a?` call

@example

Types::Error = Types.Instance(StandardError)
Types::Error = Types.Strict(StandardError)
Types.Strict(Integer) == Types::Strict::Int # => true

@param [Class,Module] klass Class or module

@return [Dry::Types::Type]

# File lib/dry/types/builder_methods.rb, line 50
def Instance(klass)
  Nominal(klass).constrained(type: klass)
end
Also aliased as: Strict
Interface(*methods) click to toggle source

Builds a constrained nominal type accepting any value that responds to given methods

@example

Types::Callable = Types.Interface(:call)
Types::Contact = Types.Interface(:name, :address)

@param methods [Array<String, Symbol>] Method names

@return [Dry::Types::Contrained]

# File lib/dry/types/builder_methods.rb, line 134
def Interface(*methods)
  methods.reduce(Types["nominal.any"]) do |type, method|
    type.constrained(respond_to: method)
  end
end
Map(key_type, value_type) click to toggle source

Build a map type

@example

Types::IntMap = Types.Map(Types::Strict::Integer, 'any')
Types::IntStringMap = Types.Map(Types::Strict::Integer, Types::Strict::String)

@param [Type] key_type Key type @param [Type] value_type Value type

@return [Dry::Types::Map]

# File lib/dry/types/builder_methods.rb, line 120
def Map(key_type, value_type)
  Nominal(::Hash).map(key_type, value_type)
end
Nominal(klass) click to toggle source

Build a nominal type

@param [Class] klass

@return [Dry::Types::Type]

# File lib/dry/types/builder_methods.rb, line 100
def Nominal(klass)
  if klass <= ::Array
    Array.new(klass)
  elsif klass <= ::Hash
    Hash.new(klass)
  else
    Nominal.new(klass)
  end
end
Strict(klass)
Alias for: Instance
Value(value) click to toggle source

Build a type with a single value The equality check done with `eql?`

@param [Object] value

@return [Dry::Types::Type]

# File lib/dry/types/builder_methods.rb, line 61
def Value(value)
  Nominal(value.class).constrained(eql: value)
end
included(base) click to toggle source

@api private

Calls superclass method
# File lib/dry/types/builder_methods.rb, line 11
def included(base)
  super
  base.extend(BuilderMethods)
end