dry-initializer 0.8.1 → 0.9.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 +23 -1
- data/dry-initializer.gemspec +1 -1
- data/lib/dry/initializer/builder.rb +2 -6
- data/lib/dry/initializer/mixin.rb +10 -34
- data/lib/dry/initializer/plugins/signature.rb +3 -4
- data/lib/dry/initializer/scope.rb +9 -20
- data/lib/dry/initializer/signature.rb +7 -15
- data/spec/options_tolerance_spec.rb +2 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c217f0fe5f51a14d0577ab5b43f9b4b976cd7c9
|
4
|
+
data.tar.gz: a1b2e181c5868340ec17aedce3804687b7b42e23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 729f4ba632a30bc9c4b9451e0c9cf1e985aff199ba4e1ddba95d07ee1beb467bb55bc5c374e0a3c0aecb21d9f9eae82e7c39f75a3c1d7aa7db82bef212e68ea6
|
7
|
+
data.tar.gz: 847c31acf6c91f7f28de176396803e2372df7d45482fc4d0df9928f7420af42ce101778f72fac55f17eeb6af518fd9aa0c6b09a2ec2da78a4468054ec0bd5282
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
## v0.9.0 2016-11-06
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* The method `#initialize` is defined when a class extended the module (@nepalez)
|
6
|
+
|
7
|
+
In previous versions the method was defined only by `param` and `option` calls.
|
8
|
+
|
9
|
+
### Breaking Changes
|
10
|
+
|
11
|
+
* The initializer accepts any option (but skips unknown) from the very beginning (@nepalez)
|
12
|
+
|
13
|
+
### Deleted
|
14
|
+
|
15
|
+
* Deprecated methods `tolerant_to_unknown_options` and `intolerant_to_unknown_options` (@nepalez)
|
16
|
+
|
17
|
+
### Internal
|
18
|
+
|
19
|
+
* Refactor scope (`using`) to support methods renaming and aliasing (@nepalez)
|
20
|
+
|
21
|
+
[Compare 0.8.1...v0.9.0](https://github.com/dry-rb/dry-initializer/compare/0.8.1...v0.9.0)
|
22
|
+
|
1
23
|
## v0.8.1 2016-11-05
|
2
24
|
|
3
25
|
### Added
|
@@ -6,7 +28,7 @@
|
|
6
28
|
|
7
29
|
option :name, Dry::Types['strict.string']
|
8
30
|
|
9
|
-
[Compare v0.8.0...
|
31
|
+
[Compare v0.8.0...0.8.1](https://github.com/dry-rb/dry-initializer/compare/v0.8.0..0.8.1)
|
10
32
|
|
11
33
|
## v0.8.0 2016-11-05
|
12
34
|
|
data/dry-initializer.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "dry-initializer"
|
3
|
-
gem.version = "0.
|
3
|
+
gem.version = "0.9.0"
|
4
4
|
gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
|
5
5
|
gem.email = ["hashtable@yandex.ru", "andrew.kozin@gmail.com"]
|
6
6
|
gem.homepage = "https://github.com/dryrb/dry-initializer"
|
@@ -76,13 +76,9 @@ module Dry::Initializer
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def reload_initializer(mixin)
|
79
|
-
strings = @parts.select { |part| String === part }
|
80
|
-
signature = [@signature.call, @tolerant].map(&:to_s)
|
81
|
-
.reject(&:empty?)
|
82
|
-
.join(", ")
|
83
79
|
mixin.class_eval <<-RUBY
|
84
|
-
def initialize(#{signature})
|
85
|
-
#{
|
80
|
+
def initialize(#{@signature.call})
|
81
|
+
#{@parts.select { |part| String === part }.join("\n")}
|
86
82
|
__after_initialize__
|
87
83
|
end
|
88
84
|
RUBY
|
@@ -14,9 +14,9 @@ module Dry::Initializer
|
|
14
14
|
# @return [self] itself
|
15
15
|
#
|
16
16
|
def param(name, type = nil, **options)
|
17
|
-
options[:type]
|
18
|
-
|
19
|
-
|
17
|
+
options[:type] = type if type
|
18
|
+
options[:option] = false
|
19
|
+
@initializer_builder = initializer_builder.define(name, **options)
|
20
20
|
initializer_builder.call(self)
|
21
21
|
end
|
22
22
|
|
@@ -27,9 +27,9 @@ module Dry::Initializer
|
|
27
27
|
# @return (see #param)
|
28
28
|
#
|
29
29
|
def option(name, type = nil, **options)
|
30
|
-
options[:type]
|
31
|
-
|
32
|
-
|
30
|
+
options[:type] = type if type
|
31
|
+
options[:option] = true
|
32
|
+
@initializer_builder = initializer_builder.define(name, **options)
|
33
33
|
initializer_builder.call(self)
|
34
34
|
end
|
35
35
|
|
@@ -54,34 +54,6 @@ module Dry::Initializer
|
|
54
54
|
initializer_builder.call(self)
|
55
55
|
end
|
56
56
|
|
57
|
-
# Makes initializer tolerant to unknown options
|
58
|
-
#
|
59
|
-
# @return [self] itself
|
60
|
-
#
|
61
|
-
def tolerant_to_unknown_options
|
62
|
-
warn <<-MESSAGE.gsub(/ +\|/, "")
|
63
|
-
|[DEPRECATION] Helper method `tolerant_to_unknown_options` was deprecated,
|
64
|
-
|and will be removed in v0.9.0.
|
65
|
-
|If any `option` was defined, the initializer becomes tolerant to all the others.
|
66
|
-
|This is necessary to support "special" names in options like `option :end`.
|
67
|
-
|It forbids options only when no one was declared explicitly.
|
68
|
-
MESSAGE
|
69
|
-
end
|
70
|
-
|
71
|
-
# Makes initializer intolerant to unknown options
|
72
|
-
#
|
73
|
-
# @return [self] itself
|
74
|
-
#
|
75
|
-
def intolerant_to_unknown_options
|
76
|
-
warn <<-MESSAGE.gsub(/ +\|/, "")
|
77
|
-
|[DEPRECATION] Helper method `intolerant_to_unknown_options` was deprecated.
|
78
|
-
|and will be removed in v0.9.0.
|
79
|
-
|If any `option` was defined, the initializer becomes tolerant to all the others.
|
80
|
-
|This is necessary to support "special" names in options like `option :end`.
|
81
|
-
|It forbids options only when no one was declared explicitly.
|
82
|
-
MESSAGE
|
83
|
-
end
|
84
|
-
|
85
57
|
private
|
86
58
|
|
87
59
|
def initializer_builder
|
@@ -91,5 +63,9 @@ module Dry::Initializer
|
|
91
63
|
def inherited(klass)
|
92
64
|
klass.instance_variable_set :@initializer_builder, initializer_builder.dup
|
93
65
|
end
|
66
|
+
|
67
|
+
def self.extended(klass)
|
68
|
+
klass.send(:initializer_builder).call(klass)
|
69
|
+
end
|
94
70
|
end
|
95
71
|
end
|
@@ -9,7 +9,7 @@ module Dry::Initializer::Plugins
|
|
9
9
|
# # => "user = Dry::Initializer::UNDEFINED"
|
10
10
|
#
|
11
11
|
# Signature.call(:user, option: true)
|
12
|
-
# # =>
|
12
|
+
# # => nil
|
13
13
|
#
|
14
14
|
class Signature < Base
|
15
15
|
def param?
|
@@ -21,9 +21,8 @@ module Dry::Initializer::Plugins
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def call
|
24
|
-
return
|
25
|
-
|
26
|
-
"#{name} = Dry::Initializer::UNDEFINED"
|
24
|
+
return unless param?
|
25
|
+
required? ? name.to_s : "#{name} = Dry::Initializer::UNDEFINED"
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
@@ -1,31 +1,20 @@
|
|
1
1
|
module Dry::Initializer
|
2
2
|
# Shared scope for several params and options
|
3
3
|
class Scope
|
4
|
-
# Defines param with shared settings
|
5
|
-
#
|
6
|
-
# @param (see Dry::Initializer::Mixin#param)
|
7
|
-
# @option (see Dry::Initializer::Mixin#param)
|
8
|
-
# @return (see Dry::Initializer::Mixin#param)
|
9
|
-
#
|
10
|
-
def param(name, **options)
|
11
|
-
@klass.param name, @options.merge(options)
|
12
|
-
end
|
13
|
-
|
14
|
-
# Defines option with shared settings
|
15
|
-
#
|
16
|
-
# @param (see Dry::Initializer::Mixin#option)
|
17
|
-
# @option (see Dry::Initializer::Mixin#option)
|
18
|
-
# @return (see Dry::Initializer::Mixin#option)
|
19
|
-
#
|
20
|
-
def option(name, **options)
|
21
|
-
@klass.option name, @options.merge(options)
|
22
|
-
end
|
23
|
-
|
24
4
|
private
|
25
5
|
|
26
6
|
def initialize(klass, **options)
|
27
7
|
@klass = klass
|
28
8
|
@options = options
|
29
9
|
end
|
10
|
+
|
11
|
+
def method_missing(name, *args, **options)
|
12
|
+
return super unless respond_to? name
|
13
|
+
@klass.send(name, *args, **@options.merge(options))
|
14
|
+
end
|
15
|
+
|
16
|
+
def respond_to_missing?(name, *)
|
17
|
+
@klass.respond_to? name
|
18
|
+
end
|
30
19
|
end
|
31
20
|
end
|
@@ -5,8 +5,8 @@ module Dry::Initializer
|
|
5
5
|
include Enumerable
|
6
6
|
include Errors
|
7
7
|
|
8
|
-
def initialize
|
9
|
-
@list =
|
8
|
+
def initialize(*list)
|
9
|
+
@list = list
|
10
10
|
end
|
11
11
|
|
12
12
|
def add(*args)
|
@@ -15,27 +15,19 @@ module Dry::Initializer
|
|
15
15
|
validates_uniqueness_of signature
|
16
16
|
validates_order_of signature
|
17
17
|
|
18
|
-
|
18
|
+
self.class.new(*@list, signature)
|
19
19
|
end
|
20
20
|
|
21
21
|
def each
|
22
|
-
|
23
|
-
yield item
|
24
|
-
end
|
22
|
+
@list.each { |item| yield item }
|
25
23
|
end
|
26
24
|
|
27
25
|
def call
|
28
|
-
map(&:call).compact.
|
26
|
+
(select(&:param?).map(&:call) + %w(**__options__)).compact.join(", ")
|
29
27
|
end
|
30
28
|
|
31
29
|
private
|
32
30
|
|
33
|
-
def copy(&block)
|
34
|
-
dup.tap do |instance|
|
35
|
-
instance.instance_eval(&block)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
31
|
def validates_uniqueness_of(signature)
|
40
32
|
return unless include? signature
|
41
33
|
|
@@ -43,8 +35,8 @@ module Dry::Initializer
|
|
43
35
|
end
|
44
36
|
|
45
37
|
def validates_order_of(signature)
|
46
|
-
return unless signature.
|
47
|
-
return unless any?
|
38
|
+
return unless signature.required? && signature.param?
|
39
|
+
return unless reject(&:required?).any?(&:param?)
|
48
40
|
|
49
41
|
fail OrderError.new(signature.name)
|
50
42
|
end
|
@@ -2,19 +2,10 @@ describe "options tolerance" do
|
|
2
2
|
before do
|
3
3
|
class Test::Foo
|
4
4
|
extend Dry::Initializer::Mixin
|
5
|
-
option :foo
|
6
5
|
end
|
7
|
-
|
8
|
-
class Test::Bar
|
9
|
-
extend Dry::Initializer::Mixin
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
it "allows unknown options when someone defined" do
|
14
|
-
expect { Test::Foo.new foo: :bar, bar: :baz }.not_to raise_error
|
15
6
|
end
|
16
7
|
|
17
|
-
it "
|
18
|
-
expect { Test::
|
8
|
+
it "allows options before any definition" do
|
9
|
+
expect { Test::Foo.new bar: :baz }.not_to raise_error
|
19
10
|
end
|
20
11
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-initializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Kochnev (marshall-lee)
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-11-
|
12
|
+
date: 2016-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|