dry-logic 0.4.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +214 -53
  3. data/LICENSE +1 -1
  4. data/README.md +14 -18
  5. data/dry-logic.gemspec +28 -15
  6. data/lib/dry-logic.rb +3 -1
  7. data/lib/dry/logic.rb +4 -2
  8. data/lib/dry/logic/appliable.rb +2 -0
  9. data/lib/dry/logic/evaluator.rb +3 -1
  10. data/lib/dry/logic/operations.rb +13 -11
  11. data/lib/dry/logic/operations/abstract.rb +8 -6
  12. data/lib/dry/logic/operations/and.rb +15 -3
  13. data/lib/dry/logic/operations/attr.rb +3 -1
  14. data/lib/dry/logic/operations/binary.rb +3 -1
  15. data/lib/dry/logic/operations/check.rb +6 -4
  16. data/lib/dry/logic/operations/each.rb +4 -2
  17. data/lib/dry/logic/operations/implication.rb +4 -2
  18. data/lib/dry/logic/operations/key.rb +11 -9
  19. data/lib/dry/logic/operations/negation.rb +9 -3
  20. data/lib/dry/logic/operations/or.rb +4 -2
  21. data/lib/dry/logic/operations/set.rb +5 -3
  22. data/lib/dry/logic/operations/unary.rb +3 -1
  23. data/lib/dry/logic/operations/xor.rb +4 -2
  24. data/lib/dry/logic/operators.rb +6 -4
  25. data/lib/dry/logic/predicates.rb +72 -28
  26. data/lib/dry/logic/result.rb +4 -2
  27. data/lib/dry/logic/rule.rb +35 -30
  28. data/lib/dry/logic/rule/interface.rb +141 -0
  29. data/lib/dry/logic/rule/predicate.rb +9 -3
  30. data/lib/dry/logic/rule_compiler.rb +7 -3
  31. data/lib/dry/logic/version.rb +3 -1
  32. metadata +21 -151
  33. data/.codeclimate.yml +0 -23
  34. data/.gitignore +0 -8
  35. data/.rspec +0 -3
  36. data/.rubocop.yml +0 -16
  37. data/.rubocop_todo.yml +0 -7
  38. data/.travis.yml +0 -31
  39. data/CONTRIBUTING.md +0 -29
  40. data/Gemfile +0 -18
  41. data/Rakefile +0 -12
  42. data/examples/basic.rb +0 -14
  43. data/spec/integration/result_spec.rb +0 -59
  44. data/spec/integration/rule_spec.rb +0 -53
  45. data/spec/shared/predicates.rb +0 -57
  46. data/spec/shared/rule.rb +0 -67
  47. data/spec/spec_helper.rb +0 -34
  48. data/spec/support/mutant.rb +0 -9
  49. data/spec/unit/operations/and_spec.rb +0 -64
  50. data/spec/unit/operations/attr_spec.rb +0 -27
  51. data/spec/unit/operations/check_spec.rb +0 -49
  52. data/spec/unit/operations/each_spec.rb +0 -47
  53. data/spec/unit/operations/implication_spec.rb +0 -30
  54. data/spec/unit/operations/key_spec.rb +0 -119
  55. data/spec/unit/operations/negation_spec.rb +0 -40
  56. data/spec/unit/operations/or_spec.rb +0 -73
  57. data/spec/unit/operations/set_spec.rb +0 -41
  58. data/spec/unit/operations/xor_spec.rb +0 -61
  59. data/spec/unit/predicates/array_spec.rb +0 -41
  60. data/spec/unit/predicates/attr_spec.rb +0 -29
  61. data/spec/unit/predicates/bool_spec.rb +0 -34
  62. data/spec/unit/predicates/case_spec.rb +0 -33
  63. data/spec/unit/predicates/date_spec.rb +0 -31
  64. data/spec/unit/predicates/date_time_spec.rb +0 -31
  65. data/spec/unit/predicates/decimal_spec.rb +0 -32
  66. data/spec/unit/predicates/empty_spec.rb +0 -38
  67. data/spec/unit/predicates/eql_spec.rb +0 -21
  68. data/spec/unit/predicates/even_spec.rb +0 -31
  69. data/spec/unit/predicates/excluded_from_spec.rb +0 -35
  70. data/spec/unit/predicates/excludes_spec.rb +0 -56
  71. data/spec/unit/predicates/false_spec.rb +0 -35
  72. data/spec/unit/predicates/filled_spec.rb +0 -38
  73. data/spec/unit/predicates/float_spec.rb +0 -31
  74. data/spec/unit/predicates/format_spec.rb +0 -21
  75. data/spec/unit/predicates/gt_spec.rb +0 -40
  76. data/spec/unit/predicates/gteq_spec.rb +0 -40
  77. data/spec/unit/predicates/included_in_spec.rb +0 -35
  78. data/spec/unit/predicates/includes_spec.rb +0 -24
  79. data/spec/unit/predicates/int_spec.rb +0 -34
  80. data/spec/unit/predicates/key_spec.rb +0 -29
  81. data/spec/unit/predicates/lt_spec.rb +0 -40
  82. data/spec/unit/predicates/lteq_spec.rb +0 -40
  83. data/spec/unit/predicates/max_size_spec.rb +0 -49
  84. data/spec/unit/predicates/min_size_spec.rb +0 -49
  85. data/spec/unit/predicates/none_spec.rb +0 -28
  86. data/spec/unit/predicates/not_eql_spec.rb +0 -21
  87. data/spec/unit/predicates/number_spec.rb +0 -37
  88. data/spec/unit/predicates/odd_spec.rb +0 -31
  89. data/spec/unit/predicates/size_spec.rb +0 -55
  90. data/spec/unit/predicates/str_spec.rb +0 -32
  91. data/spec/unit/predicates/time_spec.rb +0 -31
  92. data/spec/unit/predicates/true_spec.rb +0 -35
  93. data/spec/unit/predicates/type_spec.rb +0 -35
  94. data/spec/unit/predicates_spec.rb +0 -23
  95. data/spec/unit/rule/predicate_spec.rb +0 -53
  96. data/spec/unit/rule_compiler_spec.rb +0 -127
  97. data/spec/unit/rule_spec.rb +0 -141
@@ -1,9 +0,0 @@
1
- module Mutant
2
- class Selector
3
- class Expression < self
4
- def call(_subject)
5
- integration.all_tests
6
- end
7
- end
8
- end
9
- end
@@ -1,64 +0,0 @@
1
- RSpec.describe Operations::And do
2
- subject(:operation) { Operations::And.new(left, right) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:left) { Rule::Predicate.new(int?) }
7
- let(:right) { Rule::Predicate.new(gt?).curry(18) }
8
-
9
- describe '#call' do
10
- it 'calls left and right' do
11
- expect(operation.(18)).to be_failure
12
- end
13
- end
14
-
15
- describe '#to_ast' do
16
- it 'returns ast' do
17
- expect(operation.to_ast).to eql(
18
- [:and, [[:predicate, [:int?, [[:input, Undefined]]]], [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]
19
- )
20
- end
21
-
22
- it 'returns result ast' do
23
- expect(operation.('18').to_ast).to eql(
24
- [:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]
25
- )
26
-
27
- expect(operation.(18).to_ast).to eql(
28
- [:predicate, [:gt?, [[:num, 18], [:input, 18]]]]
29
- )
30
- end
31
-
32
- it 'returns failure result ast' do
33
- expect(operation.with(id: :age).('18').to_ast).to eql(
34
- [:failure, [:age, [:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]]]
35
- )
36
-
37
- expect(operation.with(id: :age).(18).to_ast).to eql(
38
- [:failure, [:age, [:predicate, [:gt?, [[:num, 18], [:input, 18]]]]]]
39
- )
40
- end
41
- end
42
-
43
- describe '#and' do
44
- let(:other) { Rule::Predicate.new(lt?).curry(30) }
45
-
46
- it 'creates and with the other' do
47
- expect(operation.and(other).(31)).to be_failure
48
- end
49
- end
50
-
51
- describe '#or' do
52
- let(:other) { Rule::Predicate.new(lt?).curry(14) }
53
-
54
- it 'creates or with the other' do
55
- expect(operation.or(other).(13)).to be_success
56
- end
57
- end
58
-
59
- describe '#to_s' do
60
- it 'returns string representation' do
61
- expect(operation.to_s).to eql('int? AND gt?(18)')
62
- end
63
- end
64
- end
@@ -1,27 +0,0 @@
1
- RSpec.describe Operations::Attr do
2
- subject(:operation) { Operations::Attr.new(Rule::Predicate.new(str?), name: :name) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:model) { Struct.new(:name) }
7
-
8
- describe '#call' do
9
- it 'applies predicate to the value' do
10
- expect(operation.(model.new('Jane'))).to be_success
11
- expect(operation.(model.new(nil))).to be_failure
12
- end
13
- end
14
-
15
- describe '#and' do
16
- let(:other) { Operations::Attr.new(Rule::Predicate.new(min_size?).curry(3), name: :name) }
17
-
18
- it 'returns and where value is passed to the right' do
19
- present_and_string = operation.and(other)
20
-
21
- expect(present_and_string.(model.new('Jane'))).to be_success
22
-
23
- expect(present_and_string.(model.new('Ja'))).to be_failure
24
- expect(present_and_string.(model.new(1))).to be_failure
25
- end
26
- end
27
- end
@@ -1,49 +0,0 @@
1
- RSpec.describe Operations::Check do
2
- include_context 'predicates'
3
-
4
- describe '#call' do
5
- context 'with 1-level nesting' do
6
- subject(:operation) do
7
- Operations::Check.new(Rule::Predicate.new(eql?).curry(1), id: :compare, keys: [:num])
8
- end
9
-
10
- it 'applies predicate to args extracted from the input' do
11
- expect(operation.(num: 1)).to be_success
12
- expect(operation.(num: 2)).to be_failure
13
- end
14
- end
15
-
16
- context 'with 2-levels nesting' do
17
- subject(:operation) do
18
- Operations::Check.new(Rule::Predicate.new(eql?), id: :compare, keys: [[:nums, :left], [:nums, :right]])
19
- end
20
-
21
- it 'applies predicate to args extracted from the input' do
22
- expect(operation.(nums: { left: 1, right: 1 })).to be_success
23
- expect(operation.(nums: { left: 1, right: 2 })).to be_failure
24
- end
25
-
26
- it 'curries args properly' do
27
- result = operation.(nums: { left: 1, right: 2 })
28
-
29
- expect(result.to_ast).to eql(
30
- [:failure, [:compare, [:check, [
31
- [[:nums, :left], [:nums, :right]], [:predicate, [:eql?, [[:left, 1], [:right, 2]]]]]
32
- ]]]
33
- )
34
- end
35
- end
36
- end
37
-
38
- describe '#to_ast' do
39
- subject(:operation) do
40
- Operations::Check.new(Rule::Predicate.new(str?), keys: [:email])
41
- end
42
-
43
- it 'returns ast' do
44
- expect(operation.to_ast).to eql(
45
- [:check, [[:email], [:predicate, [:str?, [[:input, Undefined]]]]]]
46
- )
47
- end
48
- end
49
- end
@@ -1,47 +0,0 @@
1
- RSpec.describe Operations::Each do
2
- subject(:operation) { Operations::Each.new(is_string) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:is_string) { Rule::Predicate.new(str?) }
7
-
8
- describe '#call' do
9
- it 'applies its rules to all elements in the input' do
10
- expect(operation.(['Address'])).to be_success
11
-
12
- expect(operation.([nil, 'Address'])).to be_failure
13
- expect(operation.([:Address, 'Address'])).to be_failure
14
- end
15
- end
16
-
17
- describe '#to_ast' do
18
- it 'returns ast' do
19
- expect(operation.to_ast).to eql([:each, [:predicate, [:str?, [[:input, Undefined]]]]])
20
- end
21
-
22
- it 'returns result ast' do
23
- expect(operation.([nil, 12, nil]).to_ast).to eql(
24
- [:set, [
25
- [:key, [0, [:predicate, [:str?, [[:input, nil]]]]]],
26
- [:key, [1, [:predicate, [:str?, [[:input, 12]]]]]],
27
- [:key, [2, [:predicate, [:str?, [[:input, nil]]]]]]
28
- ]]
29
- )
30
- end
31
-
32
- it 'returns failure result ast' do
33
- expect(operation.with(id: :tags).([nil, 'red', 12]).to_ast).to eql(
34
- [:failure, [:tags, [:set, [
35
- [:key, [0, [:predicate, [:str?, [[:input, nil]]]]]],
36
- [:key, [2, [:predicate, [:str?, [[:input, 12]]]]]]
37
- ]]]]
38
- )
39
- end
40
- end
41
-
42
- describe '#to_s' do
43
- it 'returns string representation' do
44
- expect(operation.to_s).to eql('each(str?)')
45
- end
46
- end
47
- end
@@ -1,30 +0,0 @@
1
- RSpec.describe Operations::Implication do
2
- subject(:operation) { Operations::Implication.new(left, right) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:left) { Rule::Predicate.new(int?) }
7
- let(:right) { Rule::Predicate.new(gt?).curry(18) }
8
-
9
- describe '#call' do
10
- it 'calls left and right' do
11
- expect(operation.('19')).to be_success
12
- expect(operation.(19)).to be_success
13
- expect(operation.(18)).to be_failure
14
- end
15
- end
16
-
17
- describe '#to_ast' do
18
- it 'returns ast' do
19
- expect(operation.to_ast).to eql(
20
- [:implication, [[:predicate, [:int?, [[:input, Undefined]]]], [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]
21
- )
22
- end
23
- end
24
-
25
- describe '#to_s' do
26
- it 'returns string representation' do
27
- expect(operation.to_s).to eql('int? THEN gt?(18)')
28
- end
29
- end
30
- end
@@ -1,119 +0,0 @@
1
- RSpec.describe Operations::Key do
2
- subject(:operation) { Operations::Key.new(predicate, name: :user) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:predicate) do
7
- Rule::Predicate.new(key?).curry(:age)
8
- end
9
-
10
- describe '#call' do
11
- context 'with a plain predicate' do
12
- it 'returns a success for valid input' do
13
- expect(operation.(user: { age: 18 })).to be_success
14
- end
15
-
16
- it 'returns a failure for invalid input' do
17
- result = operation.(user: {})
18
-
19
- expect(result).to be_failure
20
-
21
- expect(result.to_ast).to eql(
22
- [:failure, [:user, [:key, [:user,
23
- [:predicate, [:key?, [[:name, :age], [:input, {}]]]]
24
- ]]]]
25
- )
26
- end
27
- end
28
-
29
- context 'with a set rule as predicate' do
30
- subject(:operation) do
31
- Operations::Key.new(predicate, name: :address)
32
- end
33
-
34
- let(:predicate) do
35
- Operations::Set.new(Rule::Predicate.new(key?).curry(:city), Rule::Predicate.new(key?).curry(:zipcode))
36
- end
37
-
38
- it 'applies set rule to the value that passes' do
39
- result = operation.(address: { city: 'NYC', zipcode: '123' })
40
-
41
- expect(result).to be_success
42
- end
43
-
44
- it 'applies set rule to the value that fails' do
45
- result = operation.(address: { city: 'NYC' })
46
-
47
- expect(result).to be_failure
48
-
49
- expect(result.to_ast).to eql(
50
- [:failure, [:address, [:key, [:address, [:set, [
51
- [:predicate, [:key?, [[:name, :zipcode], [:input, { city: 'NYC' }]]]]
52
- ]]]]]]
53
- )
54
- end
55
- end
56
-
57
- context 'with an each rule as predicate' do
58
- subject(:operation) do
59
- Operations::Key.new(predicate, name: :nums)
60
- end
61
-
62
- let(:predicate) do
63
- Operations::Each.new(Rule::Predicate.new(str?))
64
- end
65
-
66
- it 'applies each rule to the value that passses' do
67
- result = operation.(nums: %w(1 2 3))
68
-
69
- expect(result).to be_success
70
- end
71
-
72
- it 'applies each rule to the value that fails' do
73
- failure = operation.(nums: [1, '3', 3])
74
-
75
- expect(failure).to be_failure
76
-
77
- expect(failure.to_ast).to eql(
78
- [:failure, [:nums, [:key, [:nums, [:set, [
79
- [:key, [0, [:predicate, [:str?, [[:input, 1]]]]]],
80
- [:key, [2, [:predicate, [:str?, [[:input, 3]]]]]]
81
- ]]]]]]
82
- )
83
- end
84
- end
85
- end
86
-
87
- describe '#to_ast' do
88
- it 'returns ast' do
89
- expect(operation.to_ast).to eql(
90
- [:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, Undefined]]]]]]
91
- )
92
- end
93
- end
94
-
95
- describe '#and' do
96
- subject(:operation) do
97
- Operations::Key.new(Rule::Predicate.new(str?), name: [:user, :name])
98
- end
99
-
100
- let(:other) do
101
- Operations::Key.new(Rule::Predicate.new(filled?), name: [:user, :name])
102
- end
103
-
104
- it 'returns and rule where value is passed to the right' do
105
- present_and_string = operation.and(other)
106
-
107
- expect(present_and_string.(user: { name: 'Jane' })).to be_success
108
-
109
- expect(present_and_string.(user: {})).to be_failure
110
- expect(present_and_string.(user: { name: 1 })).to be_failure
111
- end
112
- end
113
-
114
- describe '#to_s' do
115
- it 'returns string representation' do
116
- expect(operation.to_s).to eql('key[user](key?(:age))')
117
- end
118
- end
119
- end
@@ -1,40 +0,0 @@
1
- RSpec.describe Operations::Negation do
2
- subject(:operation) { Operations::Negation.new(is_int) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:is_int) { Rule::Predicate.new(int?) }
7
-
8
- describe '#call' do
9
- it 'negates its rule' do
10
- expect(operation.('19')).to be_success
11
- expect(operation.(17)).to be_failure
12
- end
13
- end
14
-
15
- describe '#to_ast' do
16
- it 'returns ast' do
17
- expect(operation.to_ast).to eql(
18
- [:not, [:predicate, [:int?, [[:input, Undefined]]]]]
19
- )
20
- end
21
-
22
- it 'returns result ast' do
23
- expect(operation.(17).to_ast).to eql(
24
- [:not, [:predicate, [:int?, [[:input, 17]]]]]
25
- )
26
- end
27
-
28
- it 'returns result ast with an :id' do
29
- expect(operation.with(id: :age).(17).to_ast).to eql(
30
- [:failure, [:age, [:not, [:predicate, [:int?, [[:input, 17]]]]]]]
31
- )
32
- end
33
- end
34
-
35
- describe '#to_s' do
36
- it 'returns string representation' do
37
- expect(operation.to_s).to eql('not(int?)')
38
- end
39
- end
40
- end
@@ -1,73 +0,0 @@
1
- RSpec.describe Operations::Or do
2
- subject(:operation) { Operations::Or.new(left, right) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:left) { Rule::Predicate.new(none?) }
7
- let(:right) { Rule::Predicate.new(gt?).curry(18) }
8
-
9
- let(:other) do
10
- Rule::Predicate.new(int?) & Rule::Predicate.new(lt?).curry(14)
11
- end
12
-
13
- describe '#call' do
14
- it 'calls left and right' do
15
- expect(operation.(nil)).to be_success
16
- expect(operation.(19)).to be_success
17
- expect(operation.(18)).to be_failure
18
- end
19
- end
20
-
21
- describe '#to_ast' do
22
- it 'returns ast' do
23
- expect(operation.to_ast).to eql(
24
- [:or, [
25
- [:predicate, [:none?, [[:input, Undefined]]]],
26
- [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]
27
- ]
28
- )
29
- end
30
-
31
- it 'returns result ast' do
32
- expect(operation.(17).to_ast).to eql(
33
- [:or, [
34
- [:predicate, [:none?, [[:input, 17]]]],
35
- [:predicate, [:gt?, [[:num, 18], [:input, 17]]]]]
36
- ]
37
- )
38
- end
39
-
40
- it 'returns failure result ast' do
41
- expect(operation.with(id: :age).(17).to_ast).to eql(
42
- [:failure, [:age, [:or, [
43
- [:predicate, [:none?, [[:input, 17]]]],
44
- [:predicate, [:gt?, [[:num, 18], [:input, 17]]]]]
45
- ]]]
46
- )
47
- end
48
- end
49
-
50
- describe '#and' do
51
- it 'creates and with the other' do
52
- expect(operation.and(other).(nil)).to be_failure
53
- expect(operation.and(other).(19)).to be_failure
54
- expect(operation.and(other).(13)).to be_failure
55
- expect(operation.and(other).(14)).to be_failure
56
- end
57
- end
58
-
59
- describe '#or' do
60
- it 'creates or with the other' do
61
- expect(operation.or(other).(nil)).to be_success
62
- expect(operation.or(other).(19)).to be_success
63
- expect(operation.or(other).(13)).to be_success
64
- expect(operation.or(other).(14)).to be_failure
65
- end
66
- end
67
-
68
- describe '#to_s' do
69
- it 'returns string representation' do
70
- expect(operation.to_s).to eql('none? OR gt?(18)')
71
- end
72
- end
73
- end