-
-
Notifications
You must be signed in to change notification settings - Fork 14
Improve Dry Struct docs #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
katafrakt
wants to merge
3
commits into
main
Choose a base branch
from
dry-struct-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just saw @cllns's comment in dry-rb/dry-struct#203 about un-deprecating |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| title: Deprecated Features | ||
| --- | ||
|
|
||
| ## Value | ||
|
|
||
| :warning: `Dry::Struct::Value` is deprecated in 1.2.0. Structs are already meant to be immutable, freezing them doesn't add any value (no pun intended) beyond a bad example of defensive programming. | ||
|
|
||
| You can define value objects which will behave like structs but will be _deeply frozen_: | ||
|
|
||
| ```ruby | ||
| class Location < Dry::Struct::Value | ||
| attribute :lat, Types::Float | ||
| attribute :lng, Types::Float | ||
| end | ||
|
|
||
| loc1 = Location.new(lat: 1.23, lng: 4.56) | ||
| loc2 = Location.new(lat: 1.23, lng: 4.56) | ||
|
|
||
| loc1.frozen? # true | ||
| loc2.frozen? # true | ||
|
|
||
| loc1 == loc2 | ||
| # true | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| title: Extensions | ||
| --- | ||
|
|
||
| Dry Struct provides one extension. | ||
|
|
||
| ## super_diff | ||
|
|
||
| With [super_diff](https://github.com/splitwise/super_diff) extension you can get nicer diffs in failed expectations in RSpec. | ||
|
|
||
| ``` | ||
| expected: #<User name="Jane" age=22> | ||
| got: #<User name="Jane" age=21> | ||
|
|
||
| #<User { | ||
| name: "Jane", | ||
| - age: 22 | ||
| + age: 21 | ||
| }> | ||
| ``` | ||
|
|
||
| To use it, make sure you have `super_diff` in your Gemfile and enable the extension in your `spec_helper.rb`: | ||
|
|
||
| ```ruby | ||
| # Gemfile | ||
| gem 'super_diff', group: :test | ||
|
|
||
| # spec_helper.rb | ||
| Dry::Struct.load_extensions(:super_diff) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| title: Virtus Legacy | ||
| --- | ||
|
|
||
| Dry Struct is a successor of earlier project called [Virtus](https://github.com/solnic/virtus). Virtus gained relative popularity in Ruby world with 3.7k stars on Github and over 125M downloads on RubyGems (stats for April 2026). It was officially discontinued in 2021 in favor of Dry ecosystem. | ||
|
|
||
| ### Differences between Dry Struct and Virtus | ||
|
|
||
| Dry Struct` look somewhat similar to Virtus but there are few significant differences: | ||
|
|
||
| - Structs don't provide attribute writers and are meant to be used as "data objects" exclusively | ||
| - Handling of attribute values is provided by standalone type objects from `dry-types`, which gives you way more powerful features | ||
| - Handling of attribute hashes is provided by standalone hash schemas from `dry-types`, which means there are different types of constructors in `dry-struct` | ||
| - Structs are not designed as swiss-army knives, specific constructor types are used depending on the use case | ||
| - Struct classes quack like `dry-types`, which means you can use them in hash schemas, as array members or sum them |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally think we could still have more "why?" here, but currently I'm not sure how.