Module: Dry::Types::BuilderMethods

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

Overview

Common API for building type objects in a convenient way

Instance Method Summary collapse

Instance Method Details

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

Build an array type.

Shortcut for Array#of.

Examples:

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

Parameters:

Returns:



26
27
28
# File 'lib/dry/types/builder_methods.rb', line 26

def Array(type)
  Strict(::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:



71
72
73
# File 'lib/dry/types/builder_methods.rb', line 71

def Constant(object)
  Nominal(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:



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dry/types/builder_methods.rb', line 83

def Constructor(klass, cons = nil, &block) # rubocop:disable Metrics/PerceivedComplexity:
  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) ⇒ Dry::Types::Array

Build a hash schema

Parameters:

Returns:



35
36
37
# File 'lib/dry/types/builder_methods.rb', line 35

def Hash(type_map)
  Strict(::Hash).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.



11
12
13
14
# File 'lib/dry/types/builder_methods.rb', line 11

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

#Instance(klass) ⇒ Dry::Types::Type Also known as: Strict

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

Examples:

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

Parameters:

  • klass (Class, Module)

    Class or module

Returns:



50
51
52
# File 'lib/dry/types/builder_methods.rb', line 50

def Instance(klass)
  Nominal(klass).constrained(type: klass)
end

#Interface(*methods) ⇒ Dry::Types::Contrained

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

Examples:

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

Parameters:

  • methods (Array<String, Symbol>)

    Method names

Returns:

  • (Dry::Types::Contrained)


134
135
136
137
138
# 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) ⇒ Dry::Types::Map

Build a map type

Examples:

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

Parameters:

  • key_type (Type)

    Key type

  • value_type (Type)

    Value type

Returns:



120
121
122
# 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) ⇒ Dry::Types::Type

Build a nominal type

Parameters:

  • klass (Class)

Returns:



100
101
102
103
104
105
106
107
108
# 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

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

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

Parameters:

  • value (Object)

Returns:



61
62
63
# File 'lib/dry/types/builder_methods.rb', line 61

def Value(value)
  Nominal(value.class).constrained(eql: value)
end