Getting Started
Using Dry::Types in Your Application
Make
Dry::Typesavailable to the application by creating a namespace that includesDry::Types:module Types include Dry.Types() endReload the environment, & type
Types::Coercible::Stringin the ruby console to confirm it worked:Types::Coercible::String # => #<Dry::Types::Constructor type=#<Dry::Types::Definition primitive=String options={}>>
Creating Your First Type
Define a struct's types by passing the name & type to the
attributemethod:class User < Dry::Struct attribute :name, Types::String endDefine Custom Types in the
Typesmodule, then pass the name & type toattribute: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 endUse a
Dry::Structas a type:class Message < Dry::Struct attribute :body, Types::String attribute :to, User end