dry-initializer 0.11.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +8 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.rubocop.yml +14 -62
- data/.travis.yml +15 -11
- data/CHANGELOG.md +538 -158
- data/Gemfile +2 -2
- data/LICENSE.txt +1 -1
- data/README.md +6 -6
- data/Rakefile +2 -41
- data/benchmarks/{several_defaults.rb → compare_several_defaults.rb} +4 -4
- data/benchmarks/{without_options.rb → plain_options.rb} +21 -10
- data/benchmarks/{params.rb → plain_params.rb} +20 -9
- data/benchmarks/{with_types.rb → with_coercion.rb} +23 -16
- data/benchmarks/with_defaults.rb +19 -8
- data/benchmarks/{with_types_and_defaults.rb → with_defaults_and_coercion.rb} +21 -12
- data/dry-initializer.gemspec +4 -4
- data/lib/dry/initializer/builders/attribute.rb +81 -0
- data/lib/dry/initializer/builders/initializer.rb +61 -0
- data/lib/dry/initializer/builders/reader.rb +50 -0
- data/lib/dry/initializer/builders/signature.rb +32 -0
- data/lib/dry/initializer/builders.rb +7 -0
- data/lib/dry/initializer/config.rb +172 -0
- data/lib/dry/initializer/definition.rb +116 -0
- data/lib/dry/initializer/dispatchers.rb +44 -0
- data/lib/dry/initializer/dsl.rb +43 -0
- data/lib/dry/initializer/mixin/local.rb +19 -0
- data/lib/dry/initializer/mixin/root.rb +10 -0
- data/lib/dry/initializer/mixin.rb +8 -70
- data/lib/dry/initializer.rb +49 -12
- data/lib/tasks/benchmark.rake +41 -0
- data/lib/tasks/profile.rake +78 -0
- data/spec/attributes_spec.rb +38 -0
- data/spec/coercion_of_nil_spec.rb +25 -0
- data/spec/custom_dispatchers_spec.rb +35 -0
- data/spec/custom_initializer_spec.rb +1 -1
- data/spec/default_values_spec.rb +8 -8
- data/spec/definition_spec.rb +107 -0
- data/spec/invalid_default_spec.rb +2 -2
- data/spec/missed_default_spec.rb +3 -3
- data/spec/optional_spec.rb +35 -8
- data/spec/options_tolerance_spec.rb +1 -1
- data/spec/public_attributes_utility_spec.rb +22 -0
- data/spec/reader_spec.rb +11 -11
- data/spec/repetitive_definitions_spec.rb +41 -21
- data/spec/several_assignments_spec.rb +41 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/subclassing_spec.rb +7 -3
- data/spec/type_argument_spec.rb +1 -1
- data/spec/type_constraint_spec.rb +38 -7
- data/spec/value_coercion_via_dry_types_spec.rb +12 -4
- metadata +37 -40
- data/benchmarks/options.rb +0 -54
- data/benchmarks/params_vs_options.rb +0 -35
- data/lib/dry/initializer/builder.rb +0 -100
- data/lib/dry/initializer/errors/default_value_error.rb +0 -6
- data/lib/dry/initializer/errors/order_error.rb +0 -7
- data/lib/dry/initializer/errors/plugin_error.rb +0 -6
- data/lib/dry/initializer/errors/redefinition_error.rb +0 -5
- data/lib/dry/initializer/errors/type_constraint_error.rb +0 -5
- data/lib/dry/initializer/errors.rb +0 -10
- data/lib/dry/initializer/plugins/base.rb +0 -47
- data/lib/dry/initializer/plugins/default_proc.rb +0 -28
- data/lib/dry/initializer/plugins/signature.rb +0 -28
- data/lib/dry/initializer/plugins/type_constraint.rb +0 -21
- data/lib/dry/initializer/plugins/variable_setter.rb +0 -30
- data/lib/dry/initializer/plugins.rb +0 -10
- data/lib/dry/initializer/signature.rb +0 -61
- data/spec/base_spec.rb +0 -21
- data/spec/container_spec.rb +0 -45
- data/spec/default_nil_spec.rb +0 -17
- data/spec/plugin_registry_spec.rb +0 -45
- data/spec/renaming_options_spec.rb +0 -20
@@ -1,61 +0,0 @@
|
|
1
|
-
module Dry::Initializer
|
2
|
-
# Immutable container for chunks of code describing argument signatures.
|
3
|
-
# Responcible for building the resulting signature for the initializer args.
|
4
|
-
class Signature
|
5
|
-
include Enumerable
|
6
|
-
include Errors
|
7
|
-
|
8
|
-
def initialize(*list)
|
9
|
-
@list = list
|
10
|
-
end
|
11
|
-
|
12
|
-
def add(*args)
|
13
|
-
signature = Plugins::Signature.new(*args)
|
14
|
-
|
15
|
-
validate_order_of signature
|
16
|
-
validate_param_uniqueness_of signature
|
17
|
-
validate_option_uniqueness_of signature
|
18
|
-
validate_attribute_uniqueness_of signature
|
19
|
-
|
20
|
-
self.class.new(*@list, signature)
|
21
|
-
end
|
22
|
-
|
23
|
-
def each
|
24
|
-
@list.each { |item| yield item }
|
25
|
-
end
|
26
|
-
|
27
|
-
def call
|
28
|
-
options = all?(&:param?) ? %w(__options__={}) : %w(**__options__)
|
29
|
-
(select(&:param?).map(&:call) + options).compact.join(", ")
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def validate_param_uniqueness_of(signature)
|
35
|
-
return unless signature.param?
|
36
|
-
return unless select(&:param?).map(&:name).include? signature.name
|
37
|
-
|
38
|
-
fail RedefinitionError.new(signature.name)
|
39
|
-
end
|
40
|
-
|
41
|
-
def validate_option_uniqueness_of(signature)
|
42
|
-
return if signature.param?
|
43
|
-
return unless reject(&:param?).map(&:name).include? signature.name
|
44
|
-
|
45
|
-
fail RedefinitionError.new(signature.name)
|
46
|
-
end
|
47
|
-
|
48
|
-
def validate_attribute_uniqueness_of(signature)
|
49
|
-
return unless map(&:rename).include? signature.rename
|
50
|
-
|
51
|
-
fail RedefinitionError.new(signature.name)
|
52
|
-
end
|
53
|
-
|
54
|
-
def validate_order_of(signature)
|
55
|
-
return unless signature.required? && signature.param?
|
56
|
-
return unless reject(&:required?).any?(&:param?)
|
57
|
-
|
58
|
-
fail OrderError.new(signature.name)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
data/spec/base_spec.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
describe "base example" do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer::Mixin
|
5
|
-
|
6
|
-
param :foo
|
7
|
-
param :bar
|
8
|
-
option :baz
|
9
|
-
option :qux
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
it "instantiates attributes" do
|
14
|
-
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
|
15
|
-
|
16
|
-
expect(subject.foo).to eql 1
|
17
|
-
expect(subject.bar).to eql 2
|
18
|
-
expect(subject.baz).to eql 3
|
19
|
-
expect(subject.qux).to eql 4
|
20
|
-
end
|
21
|
-
end
|
data/spec/container_spec.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
describe "container" do
|
2
|
-
context "with block syntax" do
|
3
|
-
before do
|
4
|
-
class Test::Foo
|
5
|
-
include Dry::Initializer.define {
|
6
|
-
param :foo
|
7
|
-
param :bar
|
8
|
-
option :baz
|
9
|
-
option :qux
|
10
|
-
}
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
it "instantiates attributes" do
|
15
|
-
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
|
16
|
-
|
17
|
-
expect(subject.foo).to eql 1
|
18
|
-
expect(subject.bar).to eql 2
|
19
|
-
expect(subject.baz).to eql 3
|
20
|
-
expect(subject.qux).to eql 4
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context "with lambda syntax" do
|
25
|
-
before do
|
26
|
-
class Test::Foo
|
27
|
-
include Dry::Initializer.define -> do
|
28
|
-
param :foo
|
29
|
-
param :bar
|
30
|
-
option :baz
|
31
|
-
option :qux
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
it "instantiates attributes" do
|
37
|
-
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
|
38
|
-
|
39
|
-
expect(subject.foo).to eql 1
|
40
|
-
expect(subject.bar).to eql 2
|
41
|
-
expect(subject.baz).to eql 3
|
42
|
-
expect(subject.qux).to eql 4
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
data/spec/default_nil_spec.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
describe "default nil" do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer::Mixin
|
5
|
-
|
6
|
-
param :foo, default: proc { nil }
|
7
|
-
param :bar, default: proc { nil }
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
it "is assigned" do
|
12
|
-
subject = Test::Foo.new(1)
|
13
|
-
|
14
|
-
expect(subject.foo).to eql 1
|
15
|
-
expect(subject.bar).to be_nil
|
16
|
-
end
|
17
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
describe "plugin registry" do
|
2
|
-
before do
|
3
|
-
# Define a plugin
|
4
|
-
module Test::Stringifier
|
5
|
-
class Plugin < Dry::Initializer::Plugins::Base
|
6
|
-
def call
|
7
|
-
"@#{name} = @#{name}.to_s"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.extended(klass)
|
12
|
-
klass.register_initializer_plugin(Plugin)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
# Define superclass
|
17
|
-
class Test::Foo
|
18
|
-
extend Dry::Initializer::Mixin
|
19
|
-
|
20
|
-
param :foo
|
21
|
-
end
|
22
|
-
|
23
|
-
# Apply the plugin to the subclass
|
24
|
-
class Test::Bar < Test::Foo
|
25
|
-
extend Test::Stringifier
|
26
|
-
|
27
|
-
param :bar
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
let(:instance_of_superclass) { Test::Foo.new :FOO }
|
32
|
-
let(:instance_of_subclass) { Test::Bar.new :FOO, :BAR }
|
33
|
-
|
34
|
-
it "does not pollute superclass" do
|
35
|
-
expect(instance_of_superclass.foo).to eql :FOO
|
36
|
-
end
|
37
|
-
|
38
|
-
it "preserves declarations made in superclass" do
|
39
|
-
expect(instance_of_subclass.foo).to eql :FOO
|
40
|
-
end
|
41
|
-
|
42
|
-
it "applies plugin to new declarations" do
|
43
|
-
expect(instance_of_subclass.bar).to eql "BAR"
|
44
|
-
end
|
45
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
describe "renaming options" do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer::Mixin
|
5
|
-
|
6
|
-
option :"some foo", as: :bar
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
subject { Test::Foo.new "some foo": :BAZ }
|
11
|
-
|
12
|
-
it "renames the attribute" do
|
13
|
-
expect(subject.bar).to eq :BAZ
|
14
|
-
expect(subject).not_to respond_to :foo
|
15
|
-
end
|
16
|
-
|
17
|
-
it "renames the variable" do
|
18
|
-
expect(subject.instance_variable_get(:@bar)).to eq :BAZ
|
19
|
-
end
|
20
|
-
end
|