dry-logic 0.1.3 → 0.1.4

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: 5d49a2ea6e71727f4ae69320aa0619c2ebd96325
4
- data.tar.gz: 71414bb0ff667758b7a95fb0379025bd55465b2e
3
+ metadata.gz: d99c501f48f756fd19ec5f5c3da47041b704e97a
4
+ data.tar.gz: a8dd4c89cd7674a65b69a6d441091ba73b514bbc
5
5
  SHA512:
6
- metadata.gz: b49f6272d981acc3fe076096ab9beb5c0d8aa57c5956ca88603d6187bd64e0285ec6bda802d4bc9f39b44927b71bc8a095834ea78e91dcee2d2816483bb9c071
7
- data.tar.gz: a1f2dbdd21ddf76e986036aacc3949433103335f842ae2ee3bcd603716c744b214800ed47129a0a23aa3564b13a65c914edaab8417dfde1b7046dd3c3127cb9f
6
+ metadata.gz: 68a19a8f78af8a329e47e8b089fef34fb2b9dbdd686840568b328fe755b65f9fbd7e7d9b6324aa78d68d7bba56c61f5c1a97fa51a9f7d89d39e3cc91e7c64503
7
+ data.tar.gz: e5b68c7f5599a244f9918c5d2bc67b282983dc1e48cbd2deaaeabf55670dbbe5b05a27b5f5d9ce6655cf0de6f202eab50a08cb323e37e535aa0d80951ac60a31
@@ -1,4 +1,13 @@
1
- # v0.1.2 2016-01-27
1
+ # v0.1.4 2016-01-27
2
+
3
+ ### Added
4
+
5
+ * Support for hash-names in `Check` and `Result` which can properly resolve input
6
+ from nested results (solnic)
7
+
8
+ [Compare v0.1.3...v0.1.4](https://github.com/dryrb/dry-logic/compare/v0.1.3...v0.1.4)
9
+
10
+ # v0.1.3 2016-01-27
2
11
 
3
12
  ### Added
4
13
 
@@ -11,7 +11,14 @@ module Dry
11
11
 
12
12
  class Binary < Rule::Check
13
13
  def evaluate_input(result)
14
- keys.map { |key| result[key].input }
14
+ keys.map do |key|
15
+ if key.is_a?(Hash)
16
+ parent, child = key.to_a.flatten
17
+ result[parent].input[child]
18
+ else
19
+ result[key].input
20
+ end
21
+ end
15
22
  end
16
23
  end
17
24
 
@@ -2,18 +2,27 @@ module Dry
2
2
  module Logic
3
3
  class Rule::Result < Rule
4
4
  def call(input)
5
- result = input[name]
6
- result_input = result.input
5
+ result = if name.is_a?(Hash)
6
+ parent, _ = name.to_a.flatten
7
+ input[parent]
8
+ else
9
+ input[name]
10
+ end
7
11
 
8
12
  if result.success?
9
- Result::Wrapped.new(input, predicate.(result_input), self)
13
+ Result::Wrapped.new(input, predicate.(evaluate_input(input)), self)
10
14
  else
11
15
  result
12
16
  end
13
17
  end
14
18
 
15
19
  def evaluate_input(result)
16
- result[name].input
20
+ if name.is_a?(Hash)
21
+ parent, child = name.to_a.flatten
22
+ result[parent].input[child]
23
+ else
24
+ result[name].input
25
+ end
17
26
  end
18
27
 
19
28
  def type
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Logic
3
- VERSION = '0.1.3'.freeze
3
+ VERSION = '0.1.4'.freeze
4
4
  end
5
5
  end
@@ -5,6 +5,8 @@ RSpec.shared_examples 'predicates' do
5
5
 
6
6
  let(:str?) { Dry::Logic::Predicates[:str?] }
7
7
 
8
+ let(:hash?) { Dry::Logic::Predicates[:hash?] }
9
+
8
10
  let(:int?) { Dry::Logic::Predicates[:int?] }
9
11
 
10
12
  let(:filled?) { Dry::Logic::Predicates[:filled?] }
@@ -1,8 +1,4 @@
1
1
  RSpec.describe Rule::Check do
2
- subject(:rule) do
3
- Rule::Check::Unary.new(:name, other.(input).curry(predicate), [:name])
4
- end
5
-
6
2
  include_context 'predicates'
7
3
 
8
4
  let(:other) do
@@ -10,7 +6,11 @@ RSpec.describe Rule::Check do
10
6
  end
11
7
 
12
8
  describe '#call' do
13
- context 'when then given predicate passed' do
9
+ subject(:rule) do
10
+ Rule::Check::Unary.new(:name, other.(input).curry(predicate), [:name])
11
+ end
12
+
13
+ context 'when the given predicate passed' do
14
14
  let(:input) { 'Jane' }
15
15
  let(:predicate) { :filled? }
16
16
 
@@ -28,4 +28,29 @@ RSpec.describe Rule::Check do
28
28
  end
29
29
  end
30
30
  end
31
+
32
+ describe '#call with a nested result' do
33
+ subject(:rule) do
34
+ Rule::Check::Binary.new(:address, result, [:user, { user: :address }])
35
+ end
36
+
37
+ let(:other) { Rule::Value.new(:user, hash?) }
38
+ let(:result) { other.(input).curry(:hash?) }
39
+ let(:input) { { address: 'Earth' } }
40
+
41
+ it 'evaluates the input' do
42
+ expect(rule.(user: result).to_ary).to eql([
43
+ :input, [
44
+ :address, { address: 'Earth' },
45
+ [
46
+ [:check, [
47
+ :address, [
48
+ :input, [:user, { address: 'Earth' }, [
49
+ [:val, [:user, [:predicate, [:hash?, []]]]]]]]
50
+ ]]
51
+ ]
52
+ ]
53
+ ])
54
+ end
55
+ end
31
56
  end
@@ -6,6 +6,7 @@ RSpec.describe Dry::Logic::Rule::Result do
6
6
  include_context 'predicates'
7
7
 
8
8
  let(:is_str) { Dry::Logic::Rule::Value.new(:name, str?) }
9
+ let(:is_hash) { Dry::Logic::Rule::Value.new(:name, hash?) }
9
10
  let(:is_jane) { Dry::Logic::Rule::Result.new(:name, eql?.curry('jane')) }
10
11
  let(:is_john) { Dry::Logic::Rule::Result.new(:name, eql?.curry('john')) }
11
12
 
@@ -33,6 +34,16 @@ RSpec.describe Dry::Logic::Rule::Result do
33
34
  end
34
35
  end
35
36
 
37
+ describe '#call with nested result' do
38
+ subject(:rule) do
39
+ Dry::Logic::Rule::Result.new({ user: :address }, str?)
40
+ end
41
+
42
+ it 'evaluates the input' do
43
+ expect(rule.(user: is_hash.(address: 'Earth'))).to be_success
44
+ end
45
+ end
46
+
36
47
  describe '#and' do
37
48
  let(:conjunction) { rule.and(is_jane) }
38
49
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica