dry-logic 0.6.1 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
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,49 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#max_size?' do
5
- let(:predicate_name) { :max_size? }
6
-
7
- context 'when value size is less than n' do
8
- let(:arguments_list) do
9
- [
10
- [3, [1, 2]],
11
- [5, 'Jill'],
12
- [3, { 1 => 'st', 2 => 'nd' }],
13
- [9, 1],
14
- [6, 1..5]
15
- ]
16
- end
17
-
18
- it_behaves_like 'a passing predicate'
19
- end
20
-
21
- context 'when value size is equal to n' do
22
- let(:arguments_list) do
23
- [
24
- [2, [1, 2]],
25
- [4, 'Jill'],
26
- [2, { 1 => 'st', 2 => 'nd' }],
27
- [8, 1],
28
- [5, 1..5]
29
- ]
30
- end
31
-
32
- it_behaves_like 'a passing predicate'
33
- end
34
-
35
- context 'with value size is greater than n' do
36
- let(:arguments_list) do
37
- [
38
- [1, [1, 2]],
39
- [3, 'Jill'],
40
- [1, { 1 => 'st', 2 => 'nd' }],
41
- [7, 1],
42
- [4, 1..5]
43
- ]
44
- end
45
-
46
- it_behaves_like 'a failing predicate'
47
- end
48
- end
49
- end
@@ -1,49 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#min_size?' do
5
- let(:predicate_name) { :min_size? }
6
-
7
- context 'when value size is greater than n' do
8
- let(:arguments_list) do
9
- [
10
- [1, [1, 2]],
11
- [3, 'Jill'],
12
- [1, { 1 => 'st', 2 => 'nd' }],
13
- [7, 1],
14
- [4, 1..5]
15
- ]
16
- end
17
-
18
- it_behaves_like 'a passing predicate'
19
- end
20
-
21
- context 'when value size is equal to n' do
22
- let(:arguments_list) do
23
- [
24
- [2, [1, 2]],
25
- [4, 'Jill'],
26
- [2, { 1 => 'st', 2 => 'nd' }],
27
- [8, 1],
28
- [5, 1..5]
29
- ]
30
- end
31
-
32
- it_behaves_like 'a passing predicate'
33
- end
34
-
35
- context 'with value size is less than n' do
36
- let(:arguments_list) do
37
- [
38
- [3, [1, 2]],
39
- [5, 'Jill'],
40
- [3, { 1 => 'st', 2 => 'nd' }],
41
- [9, 1],
42
- [6, 1..5]
43
- ]
44
- end
45
-
46
- it_behaves_like 'a failing predicate'
47
- end
48
- end
49
- end
@@ -1,28 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#nil?' do
5
- let(:predicate_name) { :nil? }
6
-
7
- context 'when value is nil' do
8
- let(:arguments_list) { [[nil]] }
9
- it_behaves_like 'a passing predicate'
10
- end
11
-
12
- context 'when value is not nil' do
13
- let(:arguments_list) do
14
- [
15
- [''],
16
- [true],
17
- [false],
18
- [0],
19
- [:symbol],
20
- [[]],
21
- [{}],
22
- [String]
23
- ]
24
- end
25
- it_behaves_like 'a failing predicate'
26
- end
27
- end
28
- end
@@ -1,21 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates, '#not_eql?' do
4
- let(:predicate_name) { :not_eql? }
5
-
6
- context 'when value is equal to the arg' do
7
- let(:arguments_list) do
8
- [['Foo', 'Foo']]
9
- end
10
-
11
- it_behaves_like 'a failing predicate'
12
- end
13
-
14
- context 'with value is not equal to the arg' do
15
- let(:arguments_list) do
16
- [['Bar', 'Foo']]
17
- end
18
-
19
- it_behaves_like 'a passing predicate'
20
- end
21
- end
@@ -1,37 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#number?' do
5
- let(:predicate_name) { :number? }
6
-
7
- context 'when value is numerical' do
8
- let(:arguments_list) do
9
- [
10
- ["34"],
11
- ["1.000004"],
12
- ["0"],
13
- [4],
14
- ["-15.24"],
15
- [-3.5]
16
- ]
17
- end
18
-
19
- it_behaves_like 'a passing predicate'
20
- end
21
-
22
- context 'with value is not numerical' do
23
- let(:arguments_list) do
24
- [
25
- [''],
26
- ["-14px"],
27
- ["10,150.00"],
28
- [nil],
29
- [:symbol],
30
- [String]
31
- ]
32
- end
33
-
34
- it_behaves_like 'a failing predicate'
35
- end
36
- end
37
- end
@@ -1,31 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#odd?' do
5
- let(:predicate_name) { :odd? }
6
-
7
- context 'when value is an odd int' do
8
- let(:arguments_list) do
9
- [
10
- [13],
11
- [1],
12
- [1111]
13
- ]
14
- end
15
-
16
- it_behaves_like 'a passing predicate'
17
- end
18
-
19
- context 'with value is an even int' do
20
- let(:arguments_list) do
21
- [
22
- [0],
23
- [2],
24
- [2222]
25
- ]
26
- end
27
-
28
- it_behaves_like 'a failing predicate'
29
- end
30
- end
31
- end
@@ -1,55 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#size?' do
5
- let(:predicate_name) { :size? }
6
-
7
- context 'when value size is equal to n' do
8
- let(:arguments_list) do
9
- [
10
- [[8], 2],
11
- [4, 'Jill'],
12
- [2, { 1 => 'st', 2 => 'nd' }],
13
- [8, 8],
14
- [1..8, 5]
15
- ]
16
- end
17
-
18
- it_behaves_like 'a passing predicate'
19
- end
20
-
21
- context 'when value size is greater than n' do
22
- let(:arguments_list) do
23
- [
24
- [[1, 2], 3],
25
- [5, 'Jill'],
26
- [3, { 1 => 'st', 2 => 'nd' }],
27
- [1, 9],
28
- [1..5, 6]
29
- ]
30
- end
31
-
32
- it_behaves_like 'a failing predicate'
33
- end
34
-
35
- context 'with value size is less than n' do
36
- let(:arguments_list) do
37
- [
38
- [[1, 2], 1],
39
- [3, 'Jill'],
40
- [1, { 1 => 'st', 2 => 'nd' }],
41
- [1, 7],
42
- [1..5, 4]
43
- ]
44
- end
45
-
46
- it_behaves_like 'a failing predicate'
47
- end
48
-
49
- context 'with an unsupported size' do
50
- it 'raises an error' do
51
- expect { Predicates[:size?].call('oops', 1) }.to raise_error(ArgumentError, /oops/)
52
- end
53
- end
54
- end
55
- end
@@ -1,32 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#str?' do
5
- let(:predicate_name) { :str? }
6
-
7
- context 'when value is a string' do
8
- let(:arguments_list) do
9
- [
10
- [''],
11
- ['John']
12
- ]
13
- end
14
-
15
- it_behaves_like 'a passing predicate'
16
- end
17
-
18
- context 'with value is not a string' do
19
- let(:arguments_list) do
20
- [
21
- [[]],
22
- [{}],
23
- [nil],
24
- [:symbol],
25
- [String]
26
- ]
27
- end
28
-
29
- it_behaves_like 'a failing predicate'
30
- end
31
- end
32
- end
@@ -1,31 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#time?' do
5
- let(:predicate_name) { :time? }
6
-
7
- context 'when value is a date' do
8
- let(:arguments_list) do
9
- [[Time.now]]
10
- end
11
-
12
- it_behaves_like 'a passing predicate'
13
- end
14
-
15
- context 'with value is not an integer' do
16
- let(:arguments_list) do
17
- [
18
- [''],
19
- [[]],
20
- [{}],
21
- [nil],
22
- [:symbol],
23
- [String],
24
- [1]
25
- ]
26
- end
27
-
28
- it_behaves_like 'a failing predicate'
29
- end
30
- end
31
- end
@@ -1,35 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#true?' do
5
- let(:predicate_name) { :true? }
6
-
7
- context 'when value is a date' do
8
- let(:arguments_list) do
9
- [[true]]
10
- end
11
-
12
- it_behaves_like 'a passing predicate'
13
- end
14
-
15
- context 'with value is not true' do
16
- let(:arguments_list) do
17
- [
18
- [false],
19
- [''],
20
- [[]],
21
- [{}],
22
- [nil],
23
- [:symbol],
24
- [String],
25
- [1],
26
- [0],
27
- ['true'],
28
- ['false']
29
- ]
30
- end
31
-
32
- it_behaves_like 'a failing predicate'
33
- end
34
- end
35
- end
@@ -1,35 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Dry::Logic::Predicates do
4
- describe '#type?' do
5
- let(:predicate_name) { :type? }
6
-
7
- context 'when value has a correct type' do
8
- let(:arguments_list) do
9
- [[TrueClass, true]]
10
- end
11
-
12
- it_behaves_like 'a passing predicate'
13
- end
14
-
15
- context 'with value is not true' do
16
- let(:arguments_list) do
17
- [
18
- [TrueClass, false],
19
- [TrueClass, ''],
20
- [TrueClass, []],
21
- [TrueClass, {}],
22
- [TrueClass, nil],
23
- [TrueClass, :symbol],
24
- [TrueClass, String],
25
- [TrueClass, 1],
26
- [TrueClass, 0],
27
- [TrueClass, 'true'],
28
- [TrueClass, 'false']
29
- ]
30
- end
31
-
32
- it_behaves_like 'a failing predicate'
33
- end
34
- end
35
- end
@@ -1,23 +0,0 @@
1
- require 'dry/logic/predicates'
2
-
3
- RSpec.describe Predicates do
4
- it 'can be included in another module' do
5
- mod = Module.new { include Predicates }
6
-
7
- expect(mod[:key?]).to be_a(Method)
8
- end
9
-
10
- describe '.predicate' do
11
- it 'defines a predicate method' do
12
- mod = Module.new {
13
- include Predicates
14
-
15
- predicate(:test?) do |foo|
16
- true
17
- end
18
- }
19
-
20
- expect(mod.test?('arg')).to be(true)
21
- end
22
- end
23
- end
@@ -1,53 +0,0 @@
1
- RSpec.describe Rule::Predicate do
2
- subject(:rule) { Rule::Predicate.build(predicate) }
3
-
4
- let(:predicate) { str? }
5
-
6
- include_context 'predicates'
7
-
8
- it_behaves_like Rule
9
-
10
- describe '#name' do
11
- it 'returns predicate identifier' do
12
- expect(rule.name).to be(:str?)
13
- end
14
- end
15
-
16
- describe '#to_ast' do
17
- context 'without a result' do
18
- it 'returns rule ast' do
19
- expect(rule.to_ast).to eql([:predicate, [:str?, [[:input, Undefined]]]])
20
- end
21
-
22
- it 'returns :failure with an id' do
23
- email = rule.with(id: :email)
24
-
25
- expect(email.(11).to_ast).to eql([:failure, [:email, [:predicate, [:str?, [[:input, 11]]]]]])
26
- end
27
- end
28
-
29
- context 'with a result' do
30
- it 'returns success' do
31
- expect(rule.('foo')).to be_success
32
- end
33
-
34
- it 'returns failure ast' do
35
- expect(rule.(5).to_ast).to eql([:predicate, [:str?, [[:input, 5]]]])
36
- end
37
- end
38
-
39
- context 'with a zero-arity predicate' do
40
- let(:predicate) { Module.new { def self.test?; true; end }.method(:test?) }
41
-
42
- it 'returns ast' do
43
- expect(rule.to_ast).to eql([:predicate, [:test?, []]])
44
- end
45
- end
46
- end
47
-
48
- describe '#to_s' do
49
- it 'returns string representation' do
50
- expect(rule.curry('foo').to_s).to eql('str?("foo")')
51
- end
52
- end
53
- end