Module: Dry::Types::BuilderMethods

Defined in:
lib/dry/types/builder_methods.rb

Instance Method Summary collapse

Instance Method Details

#Array(type) ⇒ Dry::Types::Array

Build an array type. It is a shortcut for Array.of

Examples:

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

Parameters:

Returns:



19
20
21
# File 'lib/dry/types/builder_methods.rb', line 19

def Array(type)
  self::Array.of(type)
end

#Constant(object) ⇒ Dry::Types::Type

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

Parameters:

  • object (Object)

Returns:



63
64
65
# File 'lib/dry/types/builder_methods.rb', line 63

def Constant(object)
  Definition.new(object.class).constrained(is: object)
end

#Constructor(klass, cons = nil, &block) ⇒ Dry::Types::Type

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

Parameters:

  • klass (Class)
  • cons (#call, nil) (defaults to: nil)

    Value constructor

  • block (#call, nil)

    Value constructor

Returns:



76
77
78
# File 'lib/dry/types/builder_methods.rb', line 76

def Constructor(klass, cons = nil, &block)
  Definition.new(klass).constructor(cons || block || klass.method(:new))
end

#Definition(klass) ⇒ Dry::Types::Type

Build a definiton type

Parameters:

  • klass (Class)

Returns:



86
87
88
# File 'lib/dry/types/builder_methods.rb', line 86

def Definition(klass)
  Definition.new(klass)
end

#Hash(schema, type_map) ⇒ Dry::Types::Array

Build a hash schema

Parameters:

Returns:



30
31
32
# File 'lib/dry/types/builder_methods.rb', line 30

def Hash(schema, type_map)
  self::Hash.public_send(schema, type_map)
end

#included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
8
# File 'lib/dry/types/builder_methods.rb', line 5

def included(base)
  super
  base.extend(BuilderMethods)
end

#Instance(klass) ⇒ Dry::Types::Type

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

Parameters:

  • klass (Class, Module)

    Class or module

Returns:



41
42
43
# File 'lib/dry/types/builder_methods.rb', line 41

def Instance(klass)
  Definition.new(klass).constrained(type: klass)
end

#Value(value) ⇒ Dry::Types::Type

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

Parameters:

  • value (Object)

Returns:



52
53
54
# File 'lib/dry/types/builder_methods.rb', line 52

def Value(value)
  Definition.new(value.class).constrained(eql: value)
end