dry-initializer 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +12 -0
  3. data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +10 -0
  4. data/.github/ISSUE_TEMPLATE/---bug-report.md +34 -0
  5. data/.github/ISSUE_TEMPLATE/---feature-request.md +18 -0
  6. data/.github/workflows/custom_ci.yml +74 -0
  7. data/.github/workflows/docsite.yml +34 -0
  8. data/.github/workflows/sync_configs.yml +34 -0
  9. data/.gitignore +12 -0
  10. data/.rspec +4 -0
  11. data/.rubocop.yml +89 -0
  12. data/CHANGELOG.md +890 -0
  13. data/CODE_OF_CONDUCT.md +13 -0
  14. data/CONTRIBUTING.md +29 -0
  15. data/Gemfile +38 -0
  16. data/Guardfile +5 -0
  17. data/LICENSE +20 -0
  18. data/LICENSE.txt +21 -0
  19. data/README.md +89 -0
  20. data/Rakefile +8 -0
  21. data/benchmarks/compare_several_defaults.rb +82 -0
  22. data/benchmarks/plain_options.rb +63 -0
  23. data/benchmarks/plain_params.rb +84 -0
  24. data/benchmarks/with_coercion.rb +71 -0
  25. data/benchmarks/with_defaults.rb +66 -0
  26. data/benchmarks/with_defaults_and_coercion.rb +59 -0
  27. data/docsite/source/attributes.html.md +106 -0
  28. data/docsite/source/container-version.html.md +39 -0
  29. data/docsite/source/index.html.md +43 -0
  30. data/docsite/source/inheritance.html.md +43 -0
  31. data/docsite/source/optionals-and-defaults.html.md +130 -0
  32. data/docsite/source/options-tolerance.html.md +27 -0
  33. data/docsite/source/params-and-options.html.md +74 -0
  34. data/docsite/source/rails-support.html.md +101 -0
  35. data/docsite/source/readers.html.md +43 -0
  36. data/docsite/source/skip-undefined.html.md +59 -0
  37. data/docsite/source/type-constraints.html.md +160 -0
  38. data/dry-initializer.gemspec +20 -0
  39. data/lib/dry-initializer.rb +1 -0
  40. data/lib/dry/initializer.rb +61 -0
  41. data/lib/dry/initializer/builders.rb +7 -0
  42. data/lib/dry/initializer/builders/attribute.rb +81 -0
  43. data/lib/dry/initializer/builders/initializer.rb +61 -0
  44. data/lib/dry/initializer/builders/reader.rb +50 -0
  45. data/lib/dry/initializer/builders/signature.rb +32 -0
  46. data/lib/dry/initializer/config.rb +184 -0
  47. data/lib/dry/initializer/definition.rb +65 -0
  48. data/lib/dry/initializer/dispatchers.rb +112 -0
  49. data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
  50. data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
  51. data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
  52. data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
  53. data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
  54. data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
  55. data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
  56. data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
  57. data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
  58. data/lib/dry/initializer/dispatchers/wrap_type.rb +27 -0
  59. data/lib/dry/initializer/dsl.rb +43 -0
  60. data/lib/dry/initializer/mixin.rb +15 -0
  61. data/lib/dry/initializer/mixin/local.rb +19 -0
  62. data/lib/dry/initializer/mixin/root.rb +11 -0
  63. data/lib/dry/initializer/struct.rb +39 -0
  64. data/lib/dry/initializer/undefined.rb +2 -0
  65. data/lib/tasks/benchmark.rake +41 -0
  66. data/lib/tasks/profile.rake +78 -0
  67. data/spec/attributes_spec.rb +38 -0
  68. data/spec/coercion_of_nil_spec.rb +25 -0
  69. data/spec/custom_dispatchers_spec.rb +35 -0
  70. data/spec/custom_initializer_spec.rb +30 -0
  71. data/spec/default_values_spec.rb +83 -0
  72. data/spec/definition_spec.rb +111 -0
  73. data/spec/invalid_default_spec.rb +13 -0
  74. data/spec/list_type_spec.rb +32 -0
  75. data/spec/missed_default_spec.rb +14 -0
  76. data/spec/nested_type_spec.rb +48 -0
  77. data/spec/optional_spec.rb +71 -0
  78. data/spec/options_tolerance_spec.rb +11 -0
  79. data/spec/public_attributes_utility_spec.rb +22 -0
  80. data/spec/reader_spec.rb +87 -0
  81. data/spec/repetitive_definitions_spec.rb +69 -0
  82. data/spec/several_assignments_spec.rb +41 -0
  83. data/spec/spec_helper.rb +29 -0
  84. data/spec/subclassing_spec.rb +49 -0
  85. data/spec/type_argument_spec.rb +35 -0
  86. data/spec/type_constraint_spec.rb +78 -0
  87. data/spec/value_coercion_via_dry_types_spec.rb +29 -0
  88. metadata +209 -0
@@ -0,0 +1,13 @@
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)
@@ -0,0 +1,29 @@
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 ADDED
@@ -0,0 +1,38 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem"s dependencies in dry-initializer.gemspec
4
+ gemspec
5
+
6
+ unless ENV['CI'].eql?('true')
7
+ group :benchmarks do
8
+ if RUBY_VERSION < "2.4"
9
+ gem "activesupport", "< 5"
10
+ else
11
+ gem "activesupport"
12
+ end
13
+
14
+ gem "active_attr"
15
+ gem "anima"
16
+ gem "attr_extras"
17
+ gem "benchmark-ips", "~> 2.5"
18
+ gem "concord"
19
+ gem "fast_attributes"
20
+ gem "kwattr"
21
+ gem "ruby-prof", platform: :mri
22
+ gem "value_struct"
23
+ gem "values"
24
+ gem "virtus"
25
+ end
26
+ end
27
+
28
+ group :development, :test do
29
+ gem "pry", platform: :mri
30
+ gem "pry-byebug", platform: :mri
31
+ if RUBY_VERSION >= "2.4"
32
+ gem 'ossy', git: 'https://github.com/solnic/ossy.git', branch: 'master', platform: :mri
33
+ end
34
+ end
35
+
36
+ group :test do
37
+ gem 'simplecov', require: false
38
+ end
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: "bundle exec rspec" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^spec/(spec_helper|support)}) { "spec" }
4
+ watch(%r{^lib/.+}) { "spec" }
5
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2019 dry-rb team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016-2017 Andrew Kozin (nepalez), Vladimir Kochnev (marshall-lee)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,89 @@
1
+ # dry-initializer [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/dry-initializer.svg)][gem]
4
+ [![Build Status](https://github.com/dry-rb/dry-initializer/workflows/ci/badge.svg)][ci]
5
+ [![Code Climate](https://codeclimate.com/github/dry-rb/dry-initializer/badges/gpa.svg)][codeclimate]
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/73cb64231f3fb2c86e26/test_coverage)][codeclimate]
7
+ [![Inline docs](http://inch-ci.org/github/dry-rb/dry-initializer.svg?branch=master)][inchpages]
8
+
9
+ [gem]: https://rubygems.org/gems/dry-initializer
10
+ [ci]: https://github.com/dry-rb/dry-initializer/actions?query=workflow%3Aci
11
+ [codeclimate]: https://codeclimate.com/github/dry-rb/dry-initializer
12
+ [coveralls]: https://coveralls.io/r/dry-rb/dry-initializer
13
+ [inchpages]: http://inch-ci.org/github/dry-rb/dry-initializer
14
+ [docs]: http://dry-rb.org/gems/dry-initializer/
15
+ [benchmarks]: https://github.com/dry-rb/dry-initializer/wiki
16
+ [license]: http://opensource.org/licenses/MIT
17
+ [chat]: https://dry-rb.zulipchat.com
18
+
19
+ DSL for building class initializer with params and options.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'dry-initializer'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ ```shell
32
+ $ bundle
33
+ ```
34
+
35
+ Or install it yourself as:
36
+
37
+ ```shell
38
+ $ gem install dry-initializer
39
+ ```
40
+
41
+ ## Synopsis
42
+
43
+ ```ruby
44
+ require 'dry-initializer'
45
+ require 'dry-types'
46
+
47
+ class User
48
+ extend Dry::Initializer
49
+
50
+ # Params of the initializer along with corresponding readers
51
+ param :name, proc(&:to_s)
52
+ param :role, default: -> { 'customer' }
53
+ # Options of the initializer along with corresponding readers
54
+ option :admin, default: -> { false }
55
+ option :vip, optional: true
56
+ end
57
+
58
+ # Defines the initializer with params and options
59
+ user = User.new 'Vladimir', 'admin', admin: true
60
+
61
+ # Defines readers for single attributes
62
+ user.name # => 'Vladimir'
63
+ user.role # => 'admin'
64
+ user.admin # => true
65
+ user.vip # => Dry::Initializer::UNDEFINED
66
+ ```
67
+
68
+ See full documentation on the [Dry project official site][docs]
69
+
70
+ ## Benchmarks
71
+
72
+ The `dry-initializer` is pretty fast for rubies 2.3+ [comparing to other libraries][benchmarks].
73
+
74
+ ## Compatibility
75
+
76
+ Tested under rubies [compatible to MRI 2.3+](.travis.yml).
77
+
78
+ ## Contributing
79
+
80
+ - [Fork the project](https://github.com/dry-rb/dry-initializer)
81
+ - Create your feature branch (`git checkout -b my-new-feature`)
82
+ - Add tests for it
83
+ - Commit your changes (`git commit -am '[UPDATE] Add some feature'`)
84
+ - Push to the branch (`git push origin my-new-feature`)
85
+ - Create a new Pull Request
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License][license].
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new :default
6
+
7
+ load "lib/tasks/benchmark.rake"
8
+ load "lib/tasks/profile.rake"
@@ -0,0 +1,82 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ require "dry-initializer"
4
+ class WithoutDefaults
5
+ extend Dry::Initializer
6
+
7
+ param :foo
8
+ param :bar
9
+ param :baz
10
+ end
11
+
12
+ class WithOneDefault
13
+ extend Dry::Initializer
14
+
15
+ param :foo
16
+ param :bar
17
+ param :baz, default: proc { "BAZ" }
18
+ end
19
+
20
+ class WithTwoDefaults
21
+ extend Dry::Initializer
22
+
23
+ param :foo
24
+ param :bar, default: proc { "BAR" }
25
+ param :baz, default: proc { "BAZ" }
26
+ end
27
+
28
+ class WithThreeDefaults
29
+ extend Dry::Initializer
30
+
31
+ param :foo, default: proc { "FOO" }
32
+ param :bar, default: proc { "BAR" }
33
+ param :baz, default: proc { "BAZ" }
34
+ end
35
+
36
+ puts "Benchmark for various options"
37
+
38
+ Benchmark.ips do |x|
39
+ x.config time: 15, warmup: 10
40
+
41
+ x.report("without defaults") do
42
+ WithoutDefaults.new "FOO", "BAR", "BAZ"
43
+ end
44
+
45
+ x.report("with 0 of 1 default used") do
46
+ WithOneDefault.new "FOO", "BAR", "BAZ"
47
+ end
48
+
49
+ x.report("with 1 of 1 default used") do
50
+ WithOneDefault.new "FOO", "BAR"
51
+ end
52
+
53
+ x.report("with 0 of 2 defaults used") do
54
+ WithTwoDefaults.new "FOO", "BAR", "BAZ"
55
+ end
56
+
57
+ x.report("with 1 of 2 defaults used") do
58
+ WithTwoDefaults.new "FOO", "BAR"
59
+ end
60
+
61
+ x.report("with 2 of 2 defaults used") do
62
+ WithTwoDefaults.new "FOO"
63
+ end
64
+
65
+ x.report("with 0 of 3 defaults used") do
66
+ WithThreeDefaults.new "FOO", "BAR", "BAZ"
67
+ end
68
+
69
+ x.report("with 1 of 3 defaults used") do
70
+ WithThreeDefaults.new "FOO", "BAR"
71
+ end
72
+
73
+ x.report("with 2 of 3 defaults used") do
74
+ WithThreeDefaults.new "FOO"
75
+ end
76
+
77
+ x.report("with 3 of 3 defaults used") do
78
+ WithThreeDefaults.new
79
+ end
80
+
81
+ x.compare!
82
+ end
@@ -0,0 +1,63 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ require "dry-initializer"
4
+ class DryTest
5
+ extend Dry::Initializer[undefined: false]
6
+
7
+ option :foo
8
+ option :bar
9
+ end
10
+
11
+ class DryTestUndefined
12
+ extend Dry::Initializer
13
+
14
+ option :foo
15
+ option :bar
16
+ end
17
+
18
+ class PlainRubyTest
19
+ attr_reader :foo, :bar
20
+
21
+ def initialize(options = {})
22
+ @foo = options[:foo]
23
+ @bar = options[:bar]
24
+ end
25
+ end
26
+
27
+ require "anima"
28
+ class AnimaTest
29
+ include Anima.new(:foo, :bar)
30
+ end
31
+
32
+ require "kwattr"
33
+ class KwattrTest
34
+ kwattr :foo, :bar
35
+ end
36
+
37
+ puts "Benchmark for instantiation with plain options"
38
+
39
+ Benchmark.ips do |x|
40
+ x.config time: 15, warmup: 10
41
+
42
+ x.report("plain Ruby") do
43
+ PlainRubyTest.new foo: "FOO", bar: "BAR"
44
+ end
45
+
46
+ x.report("dry-initializer") do
47
+ DryTest.new foo: "FOO", bar: "BAR"
48
+ end
49
+
50
+ x.report("dry-initializer (with UNDEFINED)") do
51
+ DryTestUndefined.new foo: "FOO", bar: "BAR"
52
+ end
53
+
54
+ x.report("anima") do
55
+ AnimaTest.new foo: "FOO", bar: "BAR"
56
+ end
57
+
58
+ x.report("kwattr") do
59
+ KwattrTest.new foo: "FOO", bar: "BAR"
60
+ end
61
+
62
+ x.compare!
63
+ end
@@ -0,0 +1,84 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ require "dry-initializer"
4
+ class DryTest
5
+ extend Dry::Initializer[undefined: false]
6
+
7
+ param :foo
8
+ param :bar
9
+ end
10
+
11
+ class DryTestUndefined
12
+ extend Dry::Initializer
13
+
14
+ param :foo
15
+ param :bar
16
+ end
17
+
18
+ class PlainRubyTest
19
+ attr_reader :foo, :bar
20
+
21
+ def initialize(foo, bar)
22
+ @foo = foo
23
+ @bar = bar
24
+ end
25
+ end
26
+
27
+ StructTest = Struct.new(:foo, :bar)
28
+
29
+ require "concord"
30
+ class ConcordTest
31
+ include Concord.new(:foo, :bar)
32
+ end
33
+
34
+ require "values"
35
+ ValueTest = Value.new(:foo, :bar)
36
+
37
+ require "value_struct"
38
+ ValueStructTest = ValueStruct.new(:foo, :bar)
39
+
40
+ require "attr_extras"
41
+ class AttrExtrasText
42
+ attr_initialize :foo, :bar
43
+ attr_reader :foo, :bar
44
+ end
45
+
46
+ puts "Benchmark for instantiation with plain params"
47
+
48
+ Benchmark.ips do |x|
49
+ x.config time: 15, warmup: 10
50
+
51
+ x.report("plain Ruby") do
52
+ PlainRubyTest.new "FOO", "BAR"
53
+ end
54
+
55
+ x.report("Core Struct") do
56
+ StructTest.new "FOO", "BAR"
57
+ end
58
+
59
+ x.report("values") do
60
+ ValueTest.new "FOO", "BAR"
61
+ end
62
+
63
+ x.report("value_struct") do
64
+ ValueStructTest.new "FOO", "BAR"
65
+ end
66
+
67
+ x.report("dry-initializer") do
68
+ DryTest.new "FOO", "BAR"
69
+ end
70
+
71
+ x.report("dry-initializer (with UNDEFINED)") do
72
+ DryTestUndefined.new "FOO", "BAR"
73
+ end
74
+
75
+ x.report("concord") do
76
+ ConcordTest.new "FOO", "BAR"
77
+ end
78
+
79
+ x.report("attr_extras") do
80
+ AttrExtrasText.new "FOO", "BAR"
81
+ end
82
+
83
+ x.compare!
84
+ end