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 +4 -4
- data/CHANGELOG.md +10 -1
- data/lib/dry/logic/rule/check.rb +8 -1
- data/lib/dry/logic/rule/result.rb +13 -4
- data/lib/dry/logic/version.rb +1 -1
- data/spec/shared/predicates.rb +2 -0
- data/spec/unit/rule/check_spec.rb +30 -5
- data/spec/unit/rule/result_spec.rb +11 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d99c501f48f756fd19ec5f5c3da47041b704e97a
|
4
|
+
data.tar.gz: a8dd4c89cd7674a65b69a6d441091ba73b514bbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68a19a8f78af8a329e47e8b089fef34fb2b9dbdd686840568b328fe755b65f9fbd7e7d9b6324aa78d68d7bba56c61f5c1a97fa51a9f7d89d39e3cc91e7c64503
|
7
|
+
data.tar.gz: e5b68c7f5599a244f9918c5d2bc67b282983dc1e48cbd2deaaeabf55670dbbe5b05a27b5f5d9ce6655cf0de6f202eab50a08cb323e37e535aa0d80951ac60a31
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
# v0.1.
|
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
|
|
data/lib/dry/logic/rule/check.rb
CHANGED
@@ -11,7 +11,14 @@ module Dry
|
|
11
11
|
|
12
12
|
class Binary < Rule::Check
|
13
13
|
def evaluate_input(result)
|
14
|
-
keys.map
|
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 =
|
6
|
-
|
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.(
|
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
|
-
|
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
|
data/lib/dry/logic/version.rb
CHANGED
data/spec/shared/predicates.rb
CHANGED
@@ -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
|
-
|
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
|
|