dry-struct 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +105 -61
- data/LICENSE +1 -1
- data/README.md +16 -12
- data/dry-struct.gemspec +26 -27
- data/lib/dry-struct.rb +2 -0
- data/lib/dry/struct.rb +14 -3
- data/lib/dry/struct/class_interface.rb +91 -34
- data/lib/dry/struct/compiler.rb +22 -0
- data/lib/dry/struct/constructor.rb +4 -24
- data/lib/dry/struct/errors.rb +13 -3
- data/lib/dry/struct/extensions.rb +2 -0
- data/lib/dry/struct/extensions/pretty_print.rb +3 -1
- data/lib/dry/struct/hashify.rb +5 -1
- data/lib/dry/struct/printer.rb +5 -0
- data/lib/dry/struct/struct_builder.rb +18 -11
- data/lib/dry/struct/sum.rb +3 -0
- data/lib/dry/struct/value.rb +4 -6
- data/lib/dry/struct/version.rb +3 -1
- metadata +36 -59
- data/.codeclimate.yml +0 -12
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
- data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -30
- data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
- data/.github/workflows/ci.yml +0 -74
- data/.github/workflows/docsite.yml +0 -34
- data/.github/workflows/sync_configs.yml +0 -34
- data/.gitignore +0 -12
- data/.rspec +0 -4
- data/.rubocop.yml +0 -95
- data/.yardopts +0 -4
- data/CODE_OF_CONDUCT.md +0 -13
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -28
- data/Rakefile +0 -10
- data/benchmarks/basic.rb +0 -57
- data/benchmarks/constrained.rb +0 -37
- data/benchmarks/profile_instantiation.rb +0 -19
- data/benchmarks/setup.rb +0 -11
- data/bin/console +0 -12
- data/bin/setup +0 -7
- data/docsite/source/index.html.md +0 -103
- data/docsite/source/nested-structs.html.md +0 -49
- data/docsite/source/recipes.html.md +0 -143
- data/log/.gitkeep +0 -0
data/.gitignore
DELETED
data/.rspec
DELETED
data/.rubocop.yml
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
# this file is managed by dry-rb/devtools project
|
2
|
-
|
3
|
-
AllCops:
|
4
|
-
TargetRubyVersion: 2.4
|
5
|
-
|
6
|
-
Style/EachWithObject:
|
7
|
-
Enabled: false
|
8
|
-
|
9
|
-
Style/StringLiterals:
|
10
|
-
Enabled: true
|
11
|
-
EnforcedStyle: single_quotes
|
12
|
-
|
13
|
-
Style/Alias:
|
14
|
-
Enabled: false
|
15
|
-
|
16
|
-
Style/LambdaCall:
|
17
|
-
Enabled: false
|
18
|
-
|
19
|
-
Style/StabbyLambdaParentheses:
|
20
|
-
Enabled: false
|
21
|
-
|
22
|
-
Style/FormatString:
|
23
|
-
Enabled: false
|
24
|
-
|
25
|
-
Style/Documentation:
|
26
|
-
Enabled: false
|
27
|
-
|
28
|
-
Layout/SpaceInLambdaLiteral:
|
29
|
-
Enabled: false
|
30
|
-
|
31
|
-
Layout/MultilineMethodCallIndentation:
|
32
|
-
Enabled: true
|
33
|
-
EnforcedStyle: indented
|
34
|
-
|
35
|
-
Metrics/LineLength:
|
36
|
-
Max: 100
|
37
|
-
|
38
|
-
Metrics/MethodLength:
|
39
|
-
Max: 22
|
40
|
-
|
41
|
-
Metrics/ClassLength:
|
42
|
-
Max: 150
|
43
|
-
|
44
|
-
Metrics/AbcSize:
|
45
|
-
Max: 20
|
46
|
-
|
47
|
-
Metrics/BlockLength:
|
48
|
-
Enabled: false
|
49
|
-
|
50
|
-
Metrics/CyclomaticComplexity:
|
51
|
-
Enabled: true
|
52
|
-
Max: 10
|
53
|
-
|
54
|
-
Lint/BooleanSymbol:
|
55
|
-
Enabled: false
|
56
|
-
|
57
|
-
Style/AccessModifierDeclarations:
|
58
|
-
Enabled: false
|
59
|
-
|
60
|
-
Style/BlockDelimiters:
|
61
|
-
Enabled: false
|
62
|
-
|
63
|
-
Layout/IndentFirstArrayElement:
|
64
|
-
EnforcedStyle: consistent
|
65
|
-
|
66
|
-
Style/ClassAndModuleChildren:
|
67
|
-
Exclude:
|
68
|
-
- "spec/**/*_spec.rb"
|
69
|
-
|
70
|
-
Lint/HandleExceptions:
|
71
|
-
Exclude:
|
72
|
-
- "spec/spec_helper.rb"
|
73
|
-
|
74
|
-
Naming/FileName:
|
75
|
-
Exclude:
|
76
|
-
- "lib/dry-*.rb"
|
77
|
-
|
78
|
-
Style/SymbolArray:
|
79
|
-
Exclude:
|
80
|
-
- "spec/**/*_spec.rb"
|
81
|
-
|
82
|
-
Style/ConditionalAssignment:
|
83
|
-
Enabled: false
|
84
|
-
|
85
|
-
Naming/MethodName:
|
86
|
-
Enabled: false
|
87
|
-
|
88
|
-
Style/AsciiComments:
|
89
|
-
Enabled: false
|
90
|
-
|
91
|
-
Style/DateTime:
|
92
|
-
Enabled: false
|
93
|
-
|
94
|
-
Style/IfUnlessModifier:
|
95
|
-
Enabled: false
|
data/.yardopts
DELETED
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# Contributor Code of Conduct
|
2
|
-
|
3
|
-
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
-
|
5
|
-
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
-
|
7
|
-
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
-
|
9
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
-
|
11
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
-
|
13
|
-
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.4.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
|
data/CONTRIBUTING.md
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# Issue Guidelines
|
2
|
-
|
3
|
-
## Reporting bugs
|
4
|
-
|
5
|
-
If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
|
6
|
-
|
7
|
-
## Reporting feature requests
|
8
|
-
|
9
|
-
Report a feature request **only after discussing it first on [discourse.dry-rb.org](https://discourse.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
|
10
|
-
|
11
|
-
## Reporting questions, support requests, ideas, concerns etc.
|
12
|
-
|
13
|
-
**PLEASE DON'T** - use [discourse.dry-rb.org](http://discourse.dry-rb.org) instead.
|
14
|
-
|
15
|
-
# Pull Request Guidelines
|
16
|
-
|
17
|
-
A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
|
18
|
-
|
19
|
-
Other requirements:
|
20
|
-
|
21
|
-
1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
|
22
|
-
2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
|
23
|
-
3) Add API documentation if it's a new feature
|
24
|
-
4) Update API documentation if it changes an existing feature
|
25
|
-
5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
|
26
|
-
|
27
|
-
# Asking for help
|
28
|
-
|
29
|
-
If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.dry-rb.org](https://discourse.dry-rb.org) or join [our chat](https://dry-rb.zulipchat.com).
|
data/Gemfile
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
|
-
|
5
|
-
gemspec
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'codeclimate-test-reporter', platform: :mri, require: false
|
9
|
-
gem 'dry-monads'
|
10
|
-
gem 'simplecov', require: false
|
11
|
-
gem 'warning'
|
12
|
-
end
|
13
|
-
|
14
|
-
group :tools do
|
15
|
-
gem 'pry'
|
16
|
-
gem 'pry-byebug', platform: :mri
|
17
|
-
gem 'ossy', git: 'https://github.com/solnic/ossy.git', branch: 'master'
|
18
|
-
end
|
19
|
-
|
20
|
-
group :benchmarks do
|
21
|
-
gem 'sqlite3'
|
22
|
-
gem 'activerecord'
|
23
|
-
gem 'benchmark-ips'
|
24
|
-
gem 'virtus'
|
25
|
-
gem 'fast_attributes'
|
26
|
-
gem 'attrio'
|
27
|
-
gem 'hotch'
|
28
|
-
end
|
data/Rakefile
DELETED
data/benchmarks/basic.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'dry/struct'
|
2
|
-
require 'virtus'
|
3
|
-
require 'fast_attributes'
|
4
|
-
require 'attrio'
|
5
|
-
require 'ostruct'
|
6
|
-
|
7
|
-
require 'benchmark/ips'
|
8
|
-
|
9
|
-
class VirtusUser
|
10
|
-
include Virtus.model
|
11
|
-
|
12
|
-
attribute :name, String
|
13
|
-
attribute :age, Integer
|
14
|
-
end
|
15
|
-
|
16
|
-
class FastUser
|
17
|
-
extend FastAttributes
|
18
|
-
|
19
|
-
define_attributes initialize: true, attributes: true do
|
20
|
-
attribute :name, String
|
21
|
-
attribute :age, Integer
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class AttrioUser
|
26
|
-
include Attrio
|
27
|
-
|
28
|
-
define_attributes do
|
29
|
-
attr :name, String
|
30
|
-
attr :age, Integer
|
31
|
-
end
|
32
|
-
|
33
|
-
def initialize(attributes = {})
|
34
|
-
self.attributes = attributes
|
35
|
-
end
|
36
|
-
|
37
|
-
def attributes=(attributes = {})
|
38
|
-
attributes.each do |attr,value|
|
39
|
-
self.send("#{attr}=", value) if self.respond_to?("#{attr}=")
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class DryStructUser < Dry::Struct
|
45
|
-
attributes(name: 'strict.string', age: 'params.integer')
|
46
|
-
end
|
47
|
-
|
48
|
-
puts DryStructUser.new(name: 'Jane', age: '21').inspect
|
49
|
-
|
50
|
-
Benchmark.ips do |x|
|
51
|
-
x.report('virtus') { VirtusUser.new(name: 'Jane', age: '21') }
|
52
|
-
x.report('fast_attributes') { FastUser.new(name: 'Jane', age: '21') }
|
53
|
-
x.report('attrio') { AttrioUser.new(name: 'Jane', age: '21') }
|
54
|
-
x.report('dry-struct') { DryStructUser.new(name: 'Jane', age: '21') }
|
55
|
-
|
56
|
-
x.compare!
|
57
|
-
end
|
data/benchmarks/constrained.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'dry/struct'
|
2
|
-
|
3
|
-
require 'active_record'
|
4
|
-
require 'benchmark/ips'
|
5
|
-
|
6
|
-
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
7
|
-
|
8
|
-
ActiveRecord::Schema.define do
|
9
|
-
create_table :users do |table|
|
10
|
-
table.column :name, :string
|
11
|
-
table.column :age, :integer
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class ARUser < ActiveRecord::Base
|
16
|
-
self.table_name = :users
|
17
|
-
end
|
18
|
-
|
19
|
-
module Types
|
20
|
-
include Dry.Types
|
21
|
-
end
|
22
|
-
|
23
|
-
class DryStructUser < Dry::Struct
|
24
|
-
attribute :id, Types::Params::Integer
|
25
|
-
attribute :name, Types::Strict::String.constrained(size: 3..64)
|
26
|
-
attribute :age, Types::Params::Integer.constrained(gt: 18)
|
27
|
-
end
|
28
|
-
|
29
|
-
puts ARUser.new(id: 1, name: 'Jane', age: '21').inspect
|
30
|
-
puts DryStructUser.new(id: 1, name: 'Jane', age: '21').inspect
|
31
|
-
|
32
|
-
Benchmark.ips do |x|
|
33
|
-
x.report('active record') { ARUser.new(id: 1, name: 'Jane', age: '21') }
|
34
|
-
x.report('dry-struct') { DryStructUser.new(id: 1, name: 'Jane', age: '21') }
|
35
|
-
|
36
|
-
x.compare!
|
37
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require_relative 'setup'
|
2
|
-
|
3
|
-
ATTR_NAMES = [:attr0, :attr1, :attr2, :attr3, :attr4, :attr5, :attr6, :attr7, :attr8, :attr9]
|
4
|
-
|
5
|
-
class Integers < Dry::Struct
|
6
|
-
ATTR_NAMES.each do |name|
|
7
|
-
attribute? name, 'coercible.integer'
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
integers = {attr0: 0, attr1: 1, attr2: 2, attr3: 3, attr4: 4, attr5: 5, attr6: 6, attr7: 7, attr8: 8, attr9: 9}
|
12
|
-
|
13
|
-
require 'pry-byebug'
|
14
|
-
|
15
|
-
profile do
|
16
|
-
1_000_000.times do
|
17
|
-
Integers.new(integers)
|
18
|
-
end
|
19
|
-
end
|
data/benchmarks/setup.rb
DELETED
data/bin/console
DELETED
data/bin/setup
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Introduction
|
3
|
-
layout: gem-single
|
4
|
-
type: gem
|
5
|
-
name: dry-struct
|
6
|
-
sections:
|
7
|
-
- nested-structs
|
8
|
-
- recipes
|
9
|
-
---
|
10
|
-
|
11
|
-
`dry-struct` is a gem built on top of `dry-types` which provides virtus-like DSL for defining typed struct classes.
|
12
|
-
|
13
|
-
### Basic Usage
|
14
|
-
|
15
|
-
You can define struct objects which will have readers for specified attributes using a simple dsl:
|
16
|
-
|
17
|
-
``` ruby
|
18
|
-
require 'dry-struct'
|
19
|
-
|
20
|
-
module Types
|
21
|
-
include Dry.Types()
|
22
|
-
end
|
23
|
-
|
24
|
-
class User < Dry::Struct
|
25
|
-
attribute :name, Types::String.optional
|
26
|
-
attribute :age, Types::Coercible::Integer
|
27
|
-
end
|
28
|
-
|
29
|
-
user = User.new(name: nil, age: '21')
|
30
|
-
|
31
|
-
user.name # nil
|
32
|
-
user.age # 21
|
33
|
-
|
34
|
-
user = User.new(name: 'Jane', age: '21')
|
35
|
-
|
36
|
-
user.name # => "Jane"
|
37
|
-
user.age # => 21
|
38
|
-
```
|
39
|
-
|
40
|
-
### Value
|
41
|
-
|
42
|
-
: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.
|
43
|
-
|
44
|
-
You can define value objects which will behave like structs but will be *deeply frozen*:
|
45
|
-
|
46
|
-
``` ruby
|
47
|
-
class Location < Dry::Struct::Value
|
48
|
-
attribute :lat, Types::Float
|
49
|
-
attribute :lng, Types::Float
|
50
|
-
end
|
51
|
-
|
52
|
-
loc1 = Location.new(lat: 1.23, lng: 4.56)
|
53
|
-
loc2 = Location.new(lat: 1.23, lng: 4.56)
|
54
|
-
|
55
|
-
loc1.frozen? # true
|
56
|
-
loc2.frozen? # true
|
57
|
-
|
58
|
-
loc1 == loc2
|
59
|
-
# true
|
60
|
-
```
|
61
|
-
|
62
|
-
### Hash Schemas
|
63
|
-
|
64
|
-
`Dry::Struct` out of the box uses [hash schemas](/gems/dry-types/1.0/hash-schemas) from `dry-types` for processing input hashes. `with_type_transform` and `with_key_transform` are exposed as `transform_types` and `transform_keys`:
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
class User < Dry::Struct
|
68
|
-
transform_keys(&:to_sym)
|
69
|
-
|
70
|
-
attribute :name, Types::String.optional
|
71
|
-
attribute :age, Types::Coercible::Integer
|
72
|
-
end
|
73
|
-
|
74
|
-
User.new('name' => 'Jane', 'age' => '21')
|
75
|
-
# => #<User name="Jane" age=21>
|
76
|
-
```
|
77
|
-
|
78
|
-
This plays nicely with inheritance, you can define a base struct for symbolizing input and then reuse it:
|
79
|
-
|
80
|
-
```ruby
|
81
|
-
class SymbolizeStruct < Dry::Struct
|
82
|
-
transform_keys(&:to_sym)
|
83
|
-
end
|
84
|
-
|
85
|
-
class User < SymbolizeStruct
|
86
|
-
attribute :name, Types::String.optional
|
87
|
-
attribute :age, Types::Coercible::Integer
|
88
|
-
end
|
89
|
-
```
|
90
|
-
|
91
|
-
### Validating data with dry-struct
|
92
|
-
|
93
|
-
Please don't. Structs are meant to work with valid input, it cannot generate error messages good enough for displaying them for a user etc. Use [`dry-validation`](/gems/dry-validation) for validating incoming data and then pass its output to structs.
|
94
|
-
|
95
|
-
### Differences between dry-struct and virtus
|
96
|
-
|
97
|
-
`dry-struct` look somewhat similar to Virtus but there are few significant differences:
|
98
|
-
|
99
|
-
* Structs don't provide attribute writers and are meant to be used as "data objects" exclusively
|
100
|
-
* Handling of attribute values is provided by standalone type objects from `dry-types`, which gives you way more powerful features
|
101
|
-
* Handling of attribute hashes is provided by standalone hash schemas from `dry-types`, which means there are different types of constructors in `dry-struct`
|
102
|
-
* Structs are not designed as swiss-army knifes, specific constructor types are used depending on the use case
|
103
|
-
* Struct classes quack like `dry-types`, which means you can use them in hash schemas, as array members or sum them
|