dry-logic 0.6.1 → 1.0.6

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.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +121 -22
  3. data/LICENSE +1 -1
  4. data/README.md +12 -14
  5. data/dry-logic.gemspec +29 -14
  6. data/lib/dry-logic.rb +2 -0
  7. data/lib/dry/logic.rb +2 -0
  8. data/lib/dry/logic/appliable.rb +2 -0
  9. data/lib/dry/logic/evaluator.rb +2 -0
  10. data/lib/dry/logic/operations.rb +2 -0
  11. data/lib/dry/logic/operations/abstract.rb +5 -3
  12. data/lib/dry/logic/operations/and.rb +3 -1
  13. data/lib/dry/logic/operations/attr.rb +2 -0
  14. data/lib/dry/logic/operations/binary.rb +2 -0
  15. data/lib/dry/logic/operations/check.rb +3 -1
  16. data/lib/dry/logic/operations/each.rb +2 -0
  17. data/lib/dry/logic/operations/implication.rb +2 -0
  18. data/lib/dry/logic/operations/key.rb +4 -2
  19. data/lib/dry/logic/operations/negation.rb +2 -0
  20. data/lib/dry/logic/operations/or.rb +2 -0
  21. data/lib/dry/logic/operations/set.rb +2 -0
  22. data/lib/dry/logic/operations/unary.rb +2 -0
  23. data/lib/dry/logic/operations/xor.rb +2 -0
  24. data/lib/dry/logic/operators.rb +2 -0
  25. data/lib/dry/logic/predicates.rb +39 -21
  26. data/lib/dry/logic/result.rb +2 -0
  27. data/lib/dry/logic/rule.rb +5 -3
  28. data/lib/dry/logic/rule/interface.rb +3 -1
  29. data/lib/dry/logic/rule/predicate.rb +2 -0
  30. data/lib/dry/logic/rule_compiler.rb +2 -0
  31. data/lib/dry/logic/version.rb +3 -1
  32. metadata +11 -128
  33. data/.codeclimate.yml +0 -15
  34. data/.gitignore +0 -8
  35. data/.rspec +0 -3
  36. data/.travis.yml +0 -31
  37. data/CONTRIBUTING.md +0 -29
  38. data/Gemfile +0 -13
  39. data/Rakefile +0 -12
  40. data/benchmarks/rule_application.rb +0 -28
  41. data/benchmarks/setup.rb +0 -11
  42. data/bin/console +0 -10
  43. data/examples/basic.rb +0 -14
  44. data/spec/integration/result_spec.rb +0 -59
  45. data/spec/integration/rule_spec.rb +0 -53
  46. data/spec/shared/predicates.rb +0 -57
  47. data/spec/shared/rule.rb +0 -72
  48. data/spec/spec_helper.rb +0 -34
  49. data/spec/support/mutant.rb +0 -9
  50. data/spec/unit/operations/and_spec.rb +0 -68
  51. data/spec/unit/operations/attr_spec.rb +0 -27
  52. data/spec/unit/operations/check_spec.rb +0 -49
  53. data/spec/unit/operations/each_spec.rb +0 -47
  54. data/spec/unit/operations/implication_spec.rb +0 -30
  55. data/spec/unit/operations/key_spec.rb +0 -133
  56. data/spec/unit/operations/negation_spec.rb +0 -49
  57. data/spec/unit/operations/or_spec.rb +0 -73
  58. data/spec/unit/operations/set_spec.rb +0 -41
  59. data/spec/unit/operations/xor_spec.rb +0 -61
  60. data/spec/unit/predicates/array_spec.rb +0 -41
  61. data/spec/unit/predicates/attr_spec.rb +0 -29
  62. data/spec/unit/predicates/bool_spec.rb +0 -34
  63. data/spec/unit/predicates/case_spec.rb +0 -33
  64. data/spec/unit/predicates/date_spec.rb +0 -31
  65. data/spec/unit/predicates/date_time_spec.rb +0 -31
  66. data/spec/unit/predicates/decimal_spec.rb +0 -32
  67. data/spec/unit/predicates/empty_spec.rb +0 -38
  68. data/spec/unit/predicates/eql_spec.rb +0 -21
  69. data/spec/unit/predicates/even_spec.rb +0 -31
  70. data/spec/unit/predicates/excluded_from_spec.rb +0 -35
  71. data/spec/unit/predicates/excludes_spec.rb +0 -56
  72. data/spec/unit/predicates/false_spec.rb +0 -35
  73. data/spec/unit/predicates/filled_spec.rb +0 -38
  74. data/spec/unit/predicates/float_spec.rb +0 -31
  75. data/spec/unit/predicates/format_spec.rb +0 -21
  76. data/spec/unit/predicates/gt_spec.rb +0 -40
  77. data/spec/unit/predicates/gteq_spec.rb +0 -40
  78. data/spec/unit/predicates/included_in_spec.rb +0 -35
  79. data/spec/unit/predicates/includes_spec.rb +0 -24
  80. data/spec/unit/predicates/int_spec.rb +0 -34
  81. data/spec/unit/predicates/key_spec.rb +0 -29
  82. data/spec/unit/predicates/lt_spec.rb +0 -40
  83. data/spec/unit/predicates/lteq_spec.rb +0 -40
  84. data/spec/unit/predicates/max_size_spec.rb +0 -49
  85. data/spec/unit/predicates/min_size_spec.rb +0 -49
  86. data/spec/unit/predicates/none_spec.rb +0 -28
  87. data/spec/unit/predicates/not_eql_spec.rb +0 -21
  88. data/spec/unit/predicates/number_spec.rb +0 -37
  89. data/spec/unit/predicates/odd_spec.rb +0 -31
  90. data/spec/unit/predicates/size_spec.rb +0 -55
  91. data/spec/unit/predicates/str_spec.rb +0 -32
  92. data/spec/unit/predicates/time_spec.rb +0 -31
  93. data/spec/unit/predicates/true_spec.rb +0 -35
  94. data/spec/unit/predicates/type_spec.rb +0 -35
  95. data/spec/unit/predicates_spec.rb +0 -23
  96. data/spec/unit/rule/predicate_spec.rb +0 -53
  97. data/spec/unit/rule_compiler_spec.rb +0 -127
  98. data/spec/unit/rule_spec.rb +0 -211
@@ -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,68 +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.build(int?) }
7
- let(:right) { Rule::Predicate.build(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.with(hints: false).('18').to_ast).to eql(
28
- [:predicate, [:int?, [[:input, '18']]]]
29
- )
30
-
31
- expect(operation.(18).to_ast).to eql(
32
- [:predicate, [:gt?, [[:num, 18], [:input, 18]]]]
33
- )
34
- end
35
-
36
- it 'returns failure result ast' do
37
- expect(operation.with(id: :age).('18').to_ast).to eql(
38
- [:failure, [:age, [:and, [[:predicate, [:int?, [[:input, '18']]]], [:hint, [:predicate, [:gt?, [[:num, 18], [:input, '18']]]]]]]]]
39
- )
40
-
41
- expect(operation.with(id: :age).(18).to_ast).to eql(
42
- [:failure, [:age, [:predicate, [:gt?, [[:num, 18], [:input, 18]]]]]]
43
- )
44
- end
45
- end
46
-
47
- describe '#and' do
48
- let(:other) { Rule::Predicate.build(lt?).curry(30) }
49
-
50
- it 'creates and with the other' do
51
- expect(operation.and(other).(31)).to be_failure
52
- end
53
- end
54
-
55
- describe '#or' do
56
- let(:other) { Rule::Predicate.build(lt?).curry(14) }
57
-
58
- it 'creates or with the other' do
59
- expect(operation.or(other).(13)).to be_success
60
- end
61
- end
62
-
63
- describe '#to_s' do
64
- it 'returns string representation' do
65
- expect(operation.to_s).to eql('int? AND gt?(18)')
66
- end
67
- end
68
- end
@@ -1,27 +0,0 @@
1
- RSpec.describe Operations::Attr do
2
- subject(:operation) { Operations::Attr.new(Rule::Predicate.build(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.build(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.build(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.build(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.build(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.build(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.build(int?) }
7
- let(:right) { Rule::Predicate.build(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,133 +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.build(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.build(key?).curry(:city), Rule::Predicate.build(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.build(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 '#ast' do
96
- it 'returns ast without the input' do
97
- expect(operation.ast).to eql(
98
- [:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, Undefined]]]]]]
99
- )
100
- end
101
-
102
- it 'returns ast with the input' do
103
- expect(operation.ast(user: 'jane')).to eql(
104
- [:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, 'jane']]]]]]
105
- )
106
- end
107
- end
108
-
109
- describe '#and' do
110
- subject(:operation) do
111
- Operations::Key.new(Rule::Predicate.build(str?), name: [:user, :name])
112
- end
113
-
114
- let(:other) do
115
- Operations::Key.new(Rule::Predicate.build(filled?), name: [:user, :name])
116
- end
117
-
118
- it 'returns and rule where value is passed to the right' do
119
- present_and_string = operation.and(other)
120
-
121
- expect(present_and_string.(user: { name: 'Jane' })).to be_success
122
-
123
- expect(present_and_string.(user: {})).to be_failure
124
- expect(present_and_string.(user: { name: 1 })).to be_failure
125
- end
126
- end
127
-
128
- describe '#to_s' do
129
- it 'returns string representation' do
130
- expect(operation.to_s).to eql('key[user](key?(:age))')
131
- end
132
- end
133
- end
@@ -1,49 +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.build(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
-
14
- context 'double negation' do
15
- subject(:double_negation) { Operations::Negation.new(operation) }
16
-
17
- it 'works as rule' do
18
- expect(double_negation.('19')).to be_failure
19
- expect(double_negation.(17)).to be_success
20
- end
21
- end
22
- end
23
-
24
- describe '#to_ast' do
25
- it 'returns ast' do
26
- expect(operation.to_ast).to eql(
27
- [:not, [:predicate, [:int?, [[:input, Undefined]]]]]
28
- )
29
- end
30
-
31
- it 'returns result ast' do
32
- expect(operation.(17).to_ast).to eql(
33
- [:not, [:predicate, [:int?, [[:input, 17]]]]]
34
- )
35
- end
36
-
37
- it 'returns result ast with an :id' do
38
- expect(operation.with(id: :age).(17).to_ast).to eql(
39
- [:failure, [:age, [:not, [:predicate, [:int?, [[:input, 17]]]]]]]
40
- )
41
- end
42
- end
43
-
44
- describe '#to_s' do
45
- it 'returns string representation' do
46
- expect(operation.to_s).to eql('not(int?)')
47
- end
48
- end
49
- 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.build(nil?) }
7
- let(:right) { Rule::Predicate.build(gt?).curry(18) }
8
-
9
- let(:other) do
10
- Rule::Predicate.build(int?) & Rule::Predicate.build(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, [:nil?, [[: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, [:nil?, [[: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, [:nil?, [[: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('nil? OR gt?(18)')
71
- end
72
- end
73
- end