dry-initializer 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +8 -0
- data/CHANGELOG.md +30 -0
- data/dry-initializer.gemspec +1 -1
- data/lib/dry/initializer/builders/attribute.rb +6 -1
- data/lib/dry/initializer/definition.rb +4 -3
- data/spec/type_constraint_spec.rb +30 -0
- 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: 2272ada187c2b221d758533b4cc63f37ea067aca
|
4
|
+
data.tar.gz: 80af7ae1bc35c976fc275a33b02bc0647ba1dd32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8507dde97e618b0b1ad710def44309be4bc48db87268c8757e3fdc8ceb4e8515a75e7b216c8076ee37e61a03bdeeaf818589ba934904b74bb5a8713a73d1ede8
|
7
|
+
data.tar.gz: 78cc7f411b550ace24555d6c489d5cbf18e3ead3c9fc46140c4f6563b68786152cead5e845db76c13c44855a73d9e4dd595e0f39f617a2fcb70f31ed6c43cb6f
|
data/.codeclimate.yml
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
engines:
|
3
3
|
rubocop:
|
4
4
|
enabled: true
|
5
|
+
checks:
|
6
|
+
Rubocop/Style/FrozenStringLiteralComment:
|
7
|
+
enabled: false
|
8
|
+
Rubocop/Style/PercentLiteralDelimiters:
|
9
|
+
enabled: false
|
10
|
+
Rubocop/Lint/UnderscorePrefixedVariableName:
|
11
|
+
enabled: false
|
5
12
|
duplication:
|
6
13
|
enabled: true
|
7
14
|
config:
|
@@ -10,6 +17,7 @@ engines:
|
|
10
17
|
exclude_paths:
|
11
18
|
- "benchmarks/**/*"
|
12
19
|
- "spec/**/*"
|
20
|
+
- "lib/tasks/**/*"
|
13
21
|
ratings:
|
14
22
|
paths:
|
15
23
|
- "lib/**/*"
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,35 @@ 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.3.0] [2017-09-19]
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Type coercer can take second argument for the initialized instance (nepalez)
|
12
|
+
This allows to wrap assigned value to the object that refers back
|
13
|
+
to the initializer instance. More verbose example:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class Location < String
|
17
|
+
attr_reader :parameter # refers back to its parameter
|
18
|
+
|
19
|
+
def initialize(name, parameter)
|
20
|
+
super(name)
|
21
|
+
@parameter = parameter
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Parameter
|
26
|
+
extend Dry::Initializer
|
27
|
+
param :name
|
28
|
+
param :location, ->(value, param) { Location.new(value, param) }
|
29
|
+
end
|
30
|
+
|
31
|
+
offset = Parameter.new "offset", location: "query"
|
32
|
+
offset.name # => "offset"
|
33
|
+
offset.location # => "query"
|
34
|
+
offset.location.parameter == offset # true
|
35
|
+
```
|
36
|
+
|
8
37
|
## [2.2.0] [2017-09-13]
|
9
38
|
|
10
39
|
### Added
|
@@ -711,3 +740,4 @@ First public release
|
|
711
740
|
[2.0.0]: https://github.com/dry-rb/dry-initializer/compare/v1.4.1...v2.0.0
|
712
741
|
[2.1.0]: https://github.com/dry-rb/dry-initializer/compare/v2.0.0...v2.1.0
|
713
742
|
[2.2.0]: https://github.com/dry-rb/dry-initializer/compare/v2.1.0...v2.2.0
|
743
|
+
[2.3.0]: https://github.com/dry-rb/dry-initializer/compare/v2.2.0...v2.3.0
|
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 = "2.
|
3
|
+
gem.version = "2.3.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"
|
@@ -65,7 +65,12 @@ module Dry::Initializer::Builders
|
|
65
65
|
|
66
66
|
def coercion_line
|
67
67
|
return unless @type
|
68
|
-
|
68
|
+
arity = @type.is_a?(Proc) ? @type.arity : @type.method(:call).arity
|
69
|
+
if arity.abs == 1
|
70
|
+
"#{@val} = #{@item}.type.call(#{@val}) unless #{@val} == #{@null}"
|
71
|
+
else
|
72
|
+
"#{@val} = #{@item}.type.call(#{@val}, self) unless #{@val} == #{@null}"
|
73
|
+
end
|
69
74
|
end
|
70
75
|
|
71
76
|
def assignment_line
|
@@ -81,10 +81,11 @@ module Dry::Initializer
|
|
81
81
|
|
82
82
|
def check_type(value)
|
83
83
|
return if value.nil?
|
84
|
-
arity =
|
85
|
-
|
84
|
+
arity = value.arity if value.is_a? Proc
|
85
|
+
arity ||= value.method(:call).arity if value.respond_to? :call
|
86
|
+
return value if [1, 2].include? arity.to_i.abs
|
86
87
|
raise TypeError,
|
87
|
-
"type of #{inspect} should respond to #call with
|
88
|
+
"type of #{inspect} should respond to #call with 1..2 arguments"
|
88
89
|
end
|
89
90
|
|
90
91
|
def check_default(value)
|
@@ -1,6 +1,36 @@
|
|
1
1
|
require "dry-types"
|
2
2
|
|
3
3
|
describe "type constraint" do
|
4
|
+
context "by a proc with 1 argument" do
|
5
|
+
before do
|
6
|
+
class Test::Foo
|
7
|
+
extend Dry::Initializer
|
8
|
+
param :foo, proc(&:to_s), optional: true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { Test::Foo.new :foo }
|
13
|
+
|
14
|
+
it "coerces a value" do
|
15
|
+
expect(subject.foo).to eq "foo"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "by a proc with 2 arguments" do
|
20
|
+
before do
|
21
|
+
class Test::Foo
|
22
|
+
extend Dry::Initializer
|
23
|
+
param :foo, proc { |val, obj| "#{obj.hash}:#{val}" }, optional: true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
subject { Test::Foo.new :foo }
|
28
|
+
|
29
|
+
it "coerces a value with self as a second argument" do
|
30
|
+
expect(subject.foo).to eq "#{subject.hash}:foo"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
4
34
|
context "by dry-type" do
|
5
35
|
before do
|
6
36
|
class Test::Foo
|
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.
|
4
|
+
version: 2.3.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-
|
12
|
+
date: 2017-09-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|