dry-initializer 0.10.2 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70dd3563c6e88c6c72a33f017f43655723faed0b
4
- data.tar.gz: 4639b3b06c099d2a87cc8eab77cd2f27bdd33cd9
3
+ metadata.gz: 604898d7c7112e84b0e097a2913e81ba54918e25
4
+ data.tar.gz: 417c9a1e41b76162b12d7ce36dd19a156ca408e2
5
5
  SHA512:
6
- metadata.gz: 8ef004b40f895bc06e9b0f38a1b1ccca77edd54ffd8945ceb85707f29ef166d7cb9e941ebbffa7f6df969291fe94ea259545a33aa0069f8a11faf3b0469250d9
7
- data.tar.gz: 697b5ed22e837011fa847c9e56081ea00b67f6486f97432b08482b43e98a528d7d4e60d6cf8671413938a01c18d08bb88d7751d961b269808008c99df8eff7f8
6
+ metadata.gz: aec2d5e6f46522584d247e1c106eaab9bcec8b2787c4a827ddb24e1f533c81b242b3c04796207f97c4cb0a179d7581146e94495296f995e31b5a9e0a6c269ccf
7
+ data.tar.gz: 0ee2c8d900d400312a8c8ddae17648e1b37d28edb61034a76b0c01e87b2ed740cb135281f391f2e3d3f6170ece71faa977772e463ed5603d8c7a1039d51a0948
data/CHANGELOG.md CHANGED
@@ -1,3 +1,38 @@
1
+ ## v0.11.0 2017-01-02
2
+
3
+ ### Added
4
+
5
+ * Support of reloading `#initializer` with `super` (@nepalez)
6
+
7
+ ### Internal
8
+
9
+ * Refactor the way [#initializer] method is (re)defined (@nepalez)
10
+
11
+ When you extend class with `Dry::Initializer::Mixin`, the initializer is
12
+ defined not "inside" the class per se, but inside the included module. The
13
+ reference to that module is stored as class-level `__initializer_mixin__`.
14
+
15
+ Mixin method [#initialize] calls another private method [#__initialize__].
16
+ It is this method to be reloaded every time you envoke a helper
17
+ `option` or `product`.
18
+
19
+ When new subclass is inherited, new mixin is added to chain of accessors,
20
+ but this time it does reload `__initialize__` only, not the `initialize`.
21
+ That is how you can safely reload initializer using `super`, but at the same
22
+ time use the whole power of dry-initializer DSL both in parent class and its
23
+ subclasses.
24
+
25
+ The whole stack of accessors looks like the following:
26
+ - Parent class mixin: `initialize` --> `__initialize__`
27
+ ^
28
+ - Parent class: `initialize`
29
+ - Subclass mixin: ^ `__initialize__`
30
+ - Subclass: `initialize`
31
+
32
+ See specification `spec/custom_initializer_spec.rb` to see how this works.
33
+
34
+ [Compare v0.10.2...v0.11.0](https://github.com/dry-rb/dry-initializer/compare/v0.10.2...v0.11.0)
35
+
1
36
  ## v0.10.2 2016-12-31
2
37
 
3
38
  ### Added
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer"
3
- gem.version = "0.10.2"
3
+ gem.version = "0.11.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"
@@ -77,12 +77,14 @@ module Dry::Initializer
77
77
 
78
78
  def reload_initializer(mixin)
79
79
  mixin.class_eval <<-RUBY, __FILE__, __LINE__ + 1
80
- def initialize(#{@signature.call})
80
+ def __initialize__(#{@signature.call})
81
81
  @__options__ = __options__
82
82
  #{@parts.select { |part| String === part }.join("\n")}
83
83
  __after_initialize__
84
84
  end
85
85
  RUBY
86
+
87
+ mixin.send :private, :__initialize__
86
88
  end
87
89
 
88
90
  def reload_callback(mixin)
@@ -14,8 +14,8 @@ module Dry::Initializer
14
14
  def param(name, type = nil, **options)
15
15
  options[:type] = type if type
16
16
  options[:option] = false
17
- @initializer_builder = initializer_builder.define(name, **options)
18
- initializer_builder.call(self)
17
+ @__initializer_builder__ = __initializer_builder__.define(name, **options)
18
+ __initializer_builder__.call(__initializer_mixin__)
19
19
  end
20
20
 
21
21
  # Declares a named argument
@@ -27,8 +27,8 @@ module Dry::Initializer
27
27
  def option(name, type = nil, **options)
28
28
  options[:type] = type if type
29
29
  options[:option] = true
30
- @initializer_builder = initializer_builder.define(name, **options)
31
- initializer_builder.call(self)
30
+ @__initializer_builder__ = __initializer_builder__.define(name, **options)
31
+ __initializer_builder__.call(__initializer_mixin__)
32
32
  end
33
33
 
34
34
  # Adds new plugin to the builder
@@ -37,24 +37,41 @@ module Dry::Initializer
37
37
  # @return [self] itself
38
38
  #
39
39
  def register_initializer_plugin(plugin)
40
- @initializer_builder = initializer_builder.register(plugin)
41
- initializer_builder.call(self)
40
+ @__initializer_builder__ = __initializer_builder__.register(plugin)
41
+ __initializer_builder__.call(__initializer_mixin__)
42
42
  end
43
43
 
44
44
  private
45
45
 
46
- def initializer_builder
47
- @initializer_builder ||= Builder.new
46
+ def __initializer_mixin__
47
+ @__initializer_mixin__ ||= Module.new do
48
+ def initialize(*args)
49
+ __initialize__(*args)
50
+ end
51
+ end
52
+ end
53
+
54
+ def __initializer_builder__
55
+ @__initializer_builder__ ||= Builder.new
48
56
  end
49
57
 
50
58
  def inherited(klass)
51
- klass.instance_variable_set :@initializer_builder, initializer_builder.dup
59
+ new_builder = __initializer_builder__.dup
60
+ klass.instance_variable_set :@__initializer_builder__, new_builder
61
+
62
+ new_mixin = Module.new
63
+ new_builder.call(new_mixin)
64
+ klass.instance_variable_set :@__initializer_mixin__, new_mixin
65
+ klass.include new_mixin
52
66
 
53
67
  super
54
68
  end
55
69
 
56
70
  def self.extended(klass)
57
- klass.send(:initializer_builder).call(klass)
71
+ super
72
+ mixin = klass.send(:__initializer_mixin__)
73
+ klass.send(:__initializer_builder__).call(mixin)
74
+ klass.include mixin
58
75
  end
59
76
  end
60
77
  end
@@ -0,0 +1,30 @@
1
+ describe "custom initializer" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer::Mixin
5
+
6
+ param :bar
7
+
8
+ def initialize(*args)
9
+ super
10
+ @bar *= 3
11
+ end
12
+ end
13
+
14
+ class Test::Baz < Test::Foo
15
+ param :qux
16
+
17
+ def initialize(*args)
18
+ super
19
+ @qux += 1
20
+ end
21
+ end
22
+ end
23
+
24
+ it "reloads the initializer" do
25
+ baz = Test::Baz.new(5, 5)
26
+
27
+ expect(baz.bar).to eq 15 # 5 * 3
28
+ expect(baz.qux).to eq 6 # 5 + 1
29
+ end
30
+ 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.10.2
4
+ version: 0.11.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-12-31 00:00:00.000000000 Z
12
+ date: 2017-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -116,6 +116,7 @@ files:
116
116
  - lib/dry/initializer/signature.rb
117
117
  - spec/base_spec.rb
118
118
  - spec/container_spec.rb
119
+ - spec/custom_initializer_spec.rb
119
120
  - spec/default_nil_spec.rb
120
121
  - spec/default_values_spec.rb
121
122
  - spec/invalid_default_spec.rb
@@ -158,6 +159,7 @@ summary: DSL for declaring params and options of the initializer
158
159
  test_files:
159
160
  - spec/base_spec.rb
160
161
  - spec/container_spec.rb
162
+ - spec/custom_initializer_spec.rb
161
163
  - spec/default_nil_spec.rb
162
164
  - spec/default_values_spec.rb
163
165
  - spec/invalid_default_spec.rb