Sha256: a508d39a5be841ba7df48ba15cfccea0cc42621bb098f8af7defa4ace71689cc

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

---
title: Getting Started
layout: gem-single
name: dry-types
---

### Using `Dry::Types` in Your Application

1. Make `Dry::Types` available to the application by creating a namespace that includes `Dry::Types`:

    ```ruby
    module Types
     include Dry.Types()
    end
    ```
   
2. Reload the environment, & type `Types::Coercible::String` in the ruby console to confirm it worked:

    ``` ruby
    Types::Coercible::String
    # => #<Dry::Types::Constructor type=#<Dry::Types::Definition primitive=String options={}>>
    ```

### Creating Your First Type

1. Define a struct's types by passing the name & type to the `attribute` method:

    ```ruby
    class User < Dry::Struct
      attribute :name, Types::String
    end
    ```

2. Define [Custom Types](/gems/dry-types/1.0/custom-types) in the `Types` module, then pass the name & type to `attribute`:

    ```ruby
    module Types
      include Dry.Types()
    
      Email = String.constrained(format: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
      Age = Integer.constrained(gt: 18)
    end
    class User < Dry::Struct
      attribute :name, Types::String
      attribute :email, Types::Email
      attribute :age, Types::Age
    end
    ```

3. Use a `Dry::Struct` as a type:

    ```ruby
    class Message < Dry::Struct
      attribute :body, Types::String
      attribute :to, User
    end
    ```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-types-1.2.0 docsite/source/getting-started.html.md