Using Dry Types in your application
-
Make the base types available to your application by defining your own module built from
Dry.Types():= Dry.Types() -
Reload the environment, & enter
Types::Coercible::Stringin your ruby console to confirm it worked:Types::Coercible::String # => #<Dry::Types[Constructor<Nominal<String> fn=Kernel.String>]>
Creating Your First Type
-
Define a struct's types by passing the name & type to the
attributemethod:attribute :name, Types::String end -
Define custom types in your types module, then pass the name & type to
attribute:= Dry.Types() = String.constrained(format: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i) = Integer.constrained(gt: 18) end attribute :name, Types::String attribute :email, Types::Email attribute :age, Types::Age end -
Use a
Dry::Structas a type:attribute :body, Types::String attribute :to, User end