dry-initializer 0.11.0 → 1.0.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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +18 -11
  4. data/CHANGELOG.md +85 -43
  5. data/benchmarks/options.rb +4 -4
  6. data/benchmarks/with_types.rb +2 -4
  7. data/benchmarks/with_types_and_defaults.rb +2 -4
  8. data/dry-initializer.gemspec +1 -1
  9. data/lib/dry/initializer.rb +77 -13
  10. data/lib/dry/initializer/attribute.rb +52 -0
  11. data/lib/dry/initializer/builder.rb +79 -72
  12. data/lib/dry/initializer/exceptions/default_value_error.rb +8 -0
  13. data/lib/dry/initializer/exceptions/params_order_error.rb +8 -0
  14. data/lib/dry/initializer/exceptions/type_constraint_error.rb +7 -0
  15. data/lib/dry/initializer/option.rb +55 -0
  16. data/lib/dry/initializer/param.rb +49 -0
  17. data/spec/missed_default_spec.rb +2 -2
  18. data/spec/optional_spec.rb +16 -6
  19. data/spec/options_var_spec.rb +39 -0
  20. data/spec/repetitive_definitions_spec.rb +38 -18
  21. data/spec/type_constraint_spec.rb +3 -3
  22. metadata +10 -18
  23. data/lib/dry/initializer/errors.rb +0 -10
  24. data/lib/dry/initializer/errors/default_value_error.rb +0 -6
  25. data/lib/dry/initializer/errors/order_error.rb +0 -7
  26. data/lib/dry/initializer/errors/plugin_error.rb +0 -6
  27. data/lib/dry/initializer/errors/redefinition_error.rb +0 -5
  28. data/lib/dry/initializer/errors/type_constraint_error.rb +0 -5
  29. data/lib/dry/initializer/mixin.rb +0 -77
  30. data/lib/dry/initializer/plugins.rb +0 -10
  31. data/lib/dry/initializer/plugins/base.rb +0 -47
  32. data/lib/dry/initializer/plugins/default_proc.rb +0 -28
  33. data/lib/dry/initializer/plugins/signature.rb +0 -28
  34. data/lib/dry/initializer/plugins/type_constraint.rb +0 -21
  35. data/lib/dry/initializer/plugins/variable_setter.rb +0 -30
  36. data/lib/dry/initializer/signature.rb +0 -61
  37. data/spec/plugin_registry_spec.rb +0 -45
@@ -1,28 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Builds a block to be evaluated by initializer (__after_initialize__)
3
- # to assign a default value to the argument
4
- class DefaultProc < Base
5
- def call
6
- return unless default
7
-
8
- ivar = :"@#{rename}"
9
- default_proc = default
10
-
11
- proc do
12
- if instance_variable_get(ivar) == Dry::Initializer::UNDEFINED
13
- instance_variable_set ivar, instance_eval(&default_proc)
14
- end
15
- end
16
- end
17
-
18
- private
19
-
20
- def default
21
- return unless settings.key? :default
22
-
23
- @default ||= settings[:default].tap do |value|
24
- fail DefaultValueError.new(name, value) unless Proc === value
25
- end
26
- end
27
- end
28
- end
@@ -1,28 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Plugin builds a chunk of code for the initializer's signature:
3
- #
4
- # @example
5
- # Signature.call(:user)
6
- # # => "user"
7
- #
8
- # Signature.call(:user, default: -> { nil })
9
- # # => "user = Dry::Initializer::UNDEFINED"
10
- #
11
- # Signature.call(:user, option: true)
12
- # # => nil
13
- #
14
- class Signature < Base
15
- def param?
16
- settings[:option] != true
17
- end
18
-
19
- def required?
20
- !settings.key?(:default) && !settings[:optional]
21
- end
22
-
23
- def call
24
- return unless param?
25
- required? ? name.to_s : "#{name} = Dry::Initializer::UNDEFINED"
26
- end
27
- end
28
- end
@@ -1,21 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Plugin builds either chunk of code for the #initializer,
3
- # or a proc for the ##__after_initialize__ callback.
4
- class TypeConstraint < Base
5
- def call
6
- return unless settings.key? :type
7
-
8
- type = settings[:type]
9
- fail TypeConstraintError.new(rename, type) unless type.respond_to? :call
10
-
11
- ivar = :"@#{rename}"
12
- lambda do |*|
13
- value = instance_variable_get(ivar)
14
-
15
- if value != Dry::Initializer::UNDEFINED
16
- instance_variable_set ivar, type.call(value)
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,30 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Plugin builds a code for variable setter:
3
- #
4
- # @example
5
- # VariableSetter.call(:user, option: false)
6
- # # => "@user = user"
7
- #
8
- # VariableSetter.call(:user, option: true)
9
- # # => "@user = __options__.fetch(:user)"
10
- #
11
- # VariableSetter.call(:user, option: true, optional: true)
12
- # # => "@user = __options__.fetch(:user, Dry::Initializer::UNDEFINED)"
13
- #
14
- class VariableSetter < Base
15
- def param?
16
- settings[:option] != true
17
- end
18
-
19
- def required?
20
- !settings.key?(:default) && !settings[:optional]
21
- end
22
-
23
- def call
24
- return "@#{name} = #{name}" if param?
25
- key = ":\"#{name}\""
26
- return "@#{rename} = __options__.fetch(#{key})" if required?
27
- "@#{rename} = __options__.fetch(#{key}, Dry::Initializer::UNDEFINED)"
28
- end
29
- end
30
- end
@@ -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
@@ -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