Sha256: 946dcf3960991007ae10c856325d279fed117aee9e0fb6ddb824c1c5581fbdb0

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

---
title: Nested Structs
layout: gem-single
name: dry-struct
---

The DSL allows to define nested structs by passing a block to `attribute`:

```ruby
class User < Dry::Struct
  attribute :name, Types::String
  attribute :address do
    attribute :city,   Types::String
    attribute :street, Types::String
  end
end

User.new(name: 'Jane', address: { city: 'London', street: 'Oxford' })
# => #<User name="Jane" address=#<User::Address city="London" street="Oxford">>

# constants for nested structs are automatically defined
User::Address
# => User::Address
```

By default, new struct classes uses `Dry::Struct` as a base class (`Dry::Struct::Value` for values). You can explicitly pass a different class:

```ruby
class User < Dry::Struct
  attribute :address, MyStruct do
    # ...
  end
end
```

It is even possible to define an array of struct:

```ruby
class User < Dry::Struct
  attribute :addresses, Types::Array do
    attribute :city,   Types::String
    attribute :street, Types::String
  end
end

# constants are still there!
User::Address
# => User::Address
```

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-struct-1.2.0 docsite/source/nested-structs.html.md
dry-struct-1.1.1 docsite/source/nested-structs.html.md
dry-struct-1.1.0 docsite/source/nested-structs.html.md