dry-initializer 2.2.0 → 2.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1ca51134d5d589e28d742d78ea453809ef53ea0
4
- data.tar.gz: e1c9fe841d80609eb35a17f2d5742bae2f9dd026
3
+ metadata.gz: 2272ada187c2b221d758533b4cc63f37ea067aca
4
+ data.tar.gz: 80af7ae1bc35c976fc275a33b02bc0647ba1dd32
5
5
  SHA512:
6
- metadata.gz: eb91104134574f2c106b813076de9798788f558c25cdd0bdadc0098fc6f90cea915d15e742d4805fcd32aed28c5e53a4801ed04397a51c8a668d69989f4dc8d4
7
- data.tar.gz: b104ca6bd0b5b46de69036754a7669b162bc88856aec29a298571bf5a988d8eee1c54c15aa0a0b774b872e12f2a596f190796a62f43ea36f333fe5fb4d3a4802
6
+ metadata.gz: 8507dde97e618b0b1ad710def44309be4bc48db87268c8757e3fdc8ceb4e8515a75e7b216c8076ee37e61a03bdeeaf818589ba934904b74bb5a8713a73d1ede8
7
+ data.tar.gz: 78cc7f411b550ace24555d6c489d5cbf18e3ead3c9fc46140c4f6563b68786152cead5e845db76c13c44855a73d9e4dd595e0f39f617a2fcb70f31ed6c43cb6f
@@ -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/**/*"
@@ -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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer"
3
- gem.version = "2.2.0"
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
- "#{@val} = #{@item}.type.call(#{@val}) unless #{@val} == #{@null}"
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 = value.respond_to?(:call) ? value.method(:call).arity : 0
85
- return value unless arity.zero? || arity > 1
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 one argument"
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.2.0
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-13 00:00:00.000000000 Z
12
+ date: 2017-09-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec