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)


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

def Array(type) = Strict(::Array).of(type)

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

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



70
# File 'lib/dry/types/builder_methods.rb', line 70

def Constant(object) = Nominal(object.class).constrained(is: object)

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

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dry/types/builder_methods.rb', line 80

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



33
# File 'lib/dry/types/builder_methods.rb', line 33

def Hash(type_map) = Strict(::Hash).schema(type_map)

#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


46
47
48
49
50
51
52
# File 'lib/dry/types/builder_methods.rb', line 46

def Instance(klass)
  unless klass.is_a?(::Module)
    raise ::ArgumentError, "Expected a class or module, got #{klass.inspect}"
  end

  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)


131
132
133
134
135
# File 'lib/dry/types/builder_methods.rb', line 131

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)


117
118
119
# File 'lib/dry/types/builder_methods.rb', line 117

def Map(key_type, value_type)
  Nominal(::Hash).map(key_type, value_type)
end

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

Build a nominal type



97
98
99
100
101
102
103
104
105
# File 'lib/dry/types/builder_methods.rb', line 97

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?`



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

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