1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
---
title: Custom Types
layout: gem-single
name: dry-types
---
There are a bunch of helpers for building your own types based on existing classes and values. These helpers are automatically defined if you're imported types in a module.
### `Types.Instance`
`Types.Instance` builds a type that checks if a value has the given class.
```ruby
range_type = Types.Instance(Range)
range_type[1..2] # => 1..2
```
### `Types.Value`
`Types.Value` builds a type that checks a value for equality (using `==`).
```ruby
valid = Types.Value('valid')
valid['valid'] # => 'valid'
valid['invalid']
# => Dry::Types::ConstraintError: "invalid" violates constraints (eql?("valid", "invalid") failed)
```
### `Types.Constant`
`Types.Constant` builds a type that checks a value for identity (using `equal?`).
```ruby
valid = Types.Constant(:valid)
valid[:valid] # => :valid
valid[:invalid]
# => Dry::Types::ConstraintError: :invalid violates constraints (is?(:valid, :invalid) failed)
```
### `Types.Constructor`
`Types.Constructor` builds a new constructor type for the given class. By default uses the `new` method as a constructor.
```ruby
user_type = Types.Constructor(User)
# It is equivalent to User.new(name: 'John')
user_type[name: 'John']
# Using a block
user_type = Types.Constructor(User) { |values| User.new(values) }
```
### `Types.Nominal`
`Types.Nominal` wraps the given class with a simple definition without any behavior attached.
```ruby
int = Types.Nominal(Integer)
int[1] # => 1
# The type doesn't have any checks
int['one'] # => 'one'
```
### `Types.Hash`
`Types.Hash` builds a new hash schema.
```ruby
# In the full form
Types::Hash.schema(name: Types::String, age: Types::Coercible::Integer)
# Using Types.Hash()
Types.Hash(:permissive, name: Types::String, age: Types::Coercible::Integer)
```
### `Types.Array`
`Types.Array` is a shortcut for `Types::Array.of`
```ruby
ListOfStrings = Types.Array(Types::String)
```
### `Types.Interface`
`Types.Interface` builds a type that checks a value responds to given methods.
```ruby
Callable = Types.Interface(:call)
Contact = Types.Interface(:name, :phone)
```
|