dry-initializer 2.3.0 → 2.4.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: 2272ada187c2b221d758533b4cc63f37ea067aca
4
- data.tar.gz: 80af7ae1bc35c976fc275a33b02bc0647ba1dd32
3
+ metadata.gz: 41bfe9bf29562e8ff2f907653002f1d1bae2e7bf
4
+ data.tar.gz: de76e955208183a7e617367e553cfb97bfbfbf75
5
5
  SHA512:
6
- metadata.gz: 8507dde97e618b0b1ad710def44309be4bc48db87268c8757e3fdc8ceb4e8515a75e7b216c8076ee37e61a03bdeeaf818589ba934904b74bb5a8713a73d1ede8
7
- data.tar.gz: 78cc7f411b550ace24555d6c489d5cbf18e3ead3c9fc46140c4f6563b68786152cead5e845db76c13c44855a73d9e4dd595e0f39f617a2fcb70f31ed6c43cb6f
6
+ metadata.gz: c86812a57b0471dcee12d5ef97495036b350317eb8fea8044a2e85bfd21b68aa5f6f627b6bce7f882533c8fdc3137d52640493e966c195bbcd03d53e8861697a
7
+ data.tar.gz: 726234048ec21a22da06de940bd25e3aae5ef0244b4489b238d856ba8a7992dab57af385fadf0a3f2553817257cf71cf56ed62ef688764507d54e66afd304e3b
data/CHANGELOG.md CHANGED
@@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
+ ## [2.4.0] [2018-02-01]
9
+
10
+ ### Added
11
+ - Dispatchers for adding syntax sugar to `param` and `options` (nepalez)
12
+
13
+ ```ruby
14
+ # Converts `integer: true` to `type: proc(&:to_i)`
15
+ dispatcher = ->(op) { op[:integer] ? op.merge(type: proc(&:to_i)) : op }
16
+ # Register a dispatcher
17
+ Dry::Initializer::Dispatchers << dispatcher
18
+ # Use syntax sugar
19
+ class User
20
+ param :id, integer: true # same as param :id, proc(&:to_i)
21
+ end
22
+ ```
23
+
8
24
  ## [2.3.0] [2017-09-19]
9
25
 
10
26
  ### Added
@@ -741,3 +757,4 @@ First public release
741
757
  [2.1.0]: https://github.com/dry-rb/dry-initializer/compare/v2.0.0...v2.1.0
742
758
  [2.2.0]: https://github.com/dry-rb/dry-initializer/compare/v2.1.0...v2.2.0
743
759
  [2.3.0]: https://github.com/dry-rb/dry-initializer/compare/v2.2.0...v2.3.0
760
+ [2.4.0]: https://github.com/dry-rb/dry-initializer/compare/v2.3.0...v2.4.0
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer"
3
- gem.version = "2.3.0"
3
+ gem.version = "2.4.0"
4
4
  gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
5
5
  gem.email = "andrew.kozin@gmail.com"
6
6
  gem.homepage = "https://github.com/dryrb/dry-initializer"
@@ -14,6 +14,7 @@ module Dry
14
14
  require_relative "initializer/builders"
15
15
  require_relative "initializer/config"
16
16
  require_relative "initializer/mixin"
17
+ require_relative "initializer/dispatchers"
17
18
 
18
19
  # Adds methods [.[]] and [.define]
19
20
  extend DSL
@@ -34,7 +35,7 @@ module Dry
34
35
  # @option opts [true, false, :protected, :public, :private] :reader
35
36
  # @return [self] itself
36
37
  def param(name, type = nil, **opts)
37
- dry_initializer.param(name, type, opts)
38
+ dry_initializer.param(name, type, Dispatchers[opts])
38
39
  self
39
40
  end
40
41
 
@@ -43,7 +44,7 @@ module Dry
43
44
  # @option (see #param)
44
45
  # @return (see #param)
45
46
  def option(name, type = nil, **opts)
46
- dry_initializer.option(name, type, opts)
47
+ dry_initializer.option(name, type, Dispatchers[opts])
47
48
  self
48
49
  end
49
50
 
@@ -135,7 +135,7 @@ module Dry::Initializer
135
135
  end
136
136
 
137
137
  def add_definition(option, name, type, opts)
138
- definition = Definition.new(option, null, name, type, opts)
138
+ definition = Definition.new(option, null, name, type, Dispatchers[opts])
139
139
  definitions[definition.source] = definition
140
140
  finalize
141
141
 
@@ -0,0 +1,44 @@
1
+ module Dry::Initializer
2
+ #
3
+ # @private
4
+ #
5
+ # Dispatchers allow adding syntax sugar to `.param` and `.option` methods.
6
+ #
7
+ # Every dispatcher should convert the source hash of options into
8
+ # the resulting hash so that you can send additional keys to the helpers.
9
+ #
10
+ # @example Add special dispatcher
11
+ #
12
+ # # Define a dispatcher for key :integer
13
+ # dispatcher = proc do |opts|
14
+ # opts.merge(type: proc(&:to_i)) if opts[:integer]
15
+ # end
16
+ #
17
+ # # Register a dispatcher
18
+ # Dry::Initializer::Dispatchers << dispatcher
19
+ #
20
+ # # Now you can use option `integer: true` instead of `type: proc(&:to_i)`
21
+ # class Foo
22
+ # extend Dry::Initializer
23
+ # param :id, integer: true
24
+ # end
25
+ #
26
+ module Dispatchers
27
+ class << self
28
+ def <<(item)
29
+ list << item
30
+ self
31
+ end
32
+
33
+ def [](options)
34
+ list.inject(options) { |opts, item| item.call(opts) }
35
+ end
36
+
37
+ private
38
+
39
+ def list
40
+ @list ||= []
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ describe "custom dispatchers" do
2
+ subject { Test::Foo.new "123" }
3
+
4
+ before do
5
+ dispatcher = ->(op) { op[:integer] ? op.merge(type: proc(&:to_i)) : op }
6
+ Dry::Initializer::Dispatchers << dispatcher
7
+ end
8
+
9
+ context "with extend syntax" do
10
+ before do
11
+ class Test::Foo
12
+ extend Dry::Initializer
13
+ param :id, integer: true
14
+ end
15
+ end
16
+
17
+ it "adds syntax sugar" do
18
+ expect(subject.id).to eq 123
19
+ end
20
+ end
21
+
22
+ context "with include syntax" do
23
+ before do
24
+ class Test::Foo
25
+ include Dry::Initializer.define -> do
26
+ param :id, integer: true
27
+ end
28
+ end
29
+ end
30
+
31
+ it "adds syntax sugar" do
32
+ expect(subject.id).to eq 123
33
+ end
34
+ end
35
+ 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: 2.3.0
4
+ version: 2.4.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: 2017-09-19 00:00:00.000000000 Z
12
+ date: 2018-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -102,6 +102,7 @@ files:
102
102
  - lib/dry/initializer/builders/signature.rb
103
103
  - lib/dry/initializer/config.rb
104
104
  - lib/dry/initializer/definition.rb
105
+ - lib/dry/initializer/dispatchers.rb
105
106
  - lib/dry/initializer/dsl.rb
106
107
  - lib/dry/initializer/mixin.rb
107
108
  - lib/dry/initializer/mixin/local.rb
@@ -109,6 +110,7 @@ files:
109
110
  - lib/tasks/benchmark.rake
110
111
  - lib/tasks/profile.rake
111
112
  - spec/attributes_spec.rb
113
+ - spec/custom_dispatchers_spec.rb
112
114
  - spec/custom_initializer_spec.rb
113
115
  - spec/default_values_spec.rb
114
116
  - spec/definition_spec.rb
@@ -145,12 +147,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
147
  version: '0'
146
148
  requirements: []
147
149
  rubyforge_project:
148
- rubygems_version: 2.6.13
150
+ rubygems_version: 2.6.14
149
151
  signing_key:
150
152
  specification_version: 4
151
153
  summary: DSL for declaring params and options of the initializer
152
154
  test_files:
153
155
  - spec/attributes_spec.rb
156
+ - spec/custom_dispatchers_spec.rb
154
157
  - spec/custom_initializer_spec.rb
155
158
  - spec/default_values_spec.rb
156
159
  - spec/definition_spec.rb
@@ -167,4 +170,3 @@ test_files:
167
170
  - spec/type_argument_spec.rb
168
171
  - spec/type_constraint_spec.rb
169
172
  - spec/value_coercion_via_dry_types_spec.rb
170
- has_rdoc: