dry-logic 1.0.2 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +129 -26
- data/LICENSE +1 -1
- data/README.md +12 -14
- data/dry-logic.gemspec +27 -14
- data/lib/dry-logic.rb +1 -1
- data/lib/dry/logic.rb +2 -2
- data/lib/dry/logic/evaluator.rb +1 -1
- data/lib/dry/logic/operations.rb +11 -11
- data/lib/dry/logic/operations/abstract.rb +6 -6
- data/lib/dry/logic/operations/and.rb +4 -4
- data/lib/dry/logic/operations/attr.rb +1 -1
- data/lib/dry/logic/operations/binary.rb +1 -1
- data/lib/dry/logic/operations/check.rb +4 -4
- data/lib/dry/logic/operations/each.rb +2 -2
- data/lib/dry/logic/operations/implication.rb +2 -2
- data/lib/dry/logic/operations/key.rb +5 -5
- data/lib/dry/logic/operations/negation.rb +2 -2
- data/lib/dry/logic/operations/or.rb +2 -2
- data/lib/dry/logic/operations/set.rb +3 -3
- data/lib/dry/logic/operations/unary.rb +1 -1
- data/lib/dry/logic/operations/xor.rb +2 -2
- data/lib/dry/logic/operators.rb +4 -4
- data/lib/dry/logic/predicates.rb +60 -28
- data/lib/dry/logic/result.rb +2 -2
- data/lib/dry/logic/rule.rb +11 -11
- data/lib/dry/logic/rule/interface.rb +32 -37
- data/lib/dry/logic/rule/predicate.rb +3 -3
- data/lib/dry/logic/rule_compiler.rb +3 -3
- data/lib/dry/logic/version.rb +1 -1
- metadata +12 -133
- data/.codeclimate.yml +0 -15
- data/.gitignore +0 -9
- data/.rspec +0 -3
- data/.travis.yml +0 -31
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -15
- data/Rakefile +0 -14
- data/benchmarks/rule_application.rb +0 -30
- data/benchmarks/setup.rb +0 -13
- data/bin/console +0 -11
- data/examples/basic.rb +0 -16
- data/spec/integration/result_spec.rb +0 -61
- data/spec/integration/rule_spec.rb +0 -55
- data/spec/shared/predicates.rb +0 -59
- data/spec/shared/rule.rb +0 -74
- data/spec/spec_helper.rb +0 -36
- data/spec/support/mutant.rb +0 -11
- data/spec/unit/operations/and_spec.rb +0 -70
- data/spec/unit/operations/attr_spec.rb +0 -29
- data/spec/unit/operations/check_spec.rb +0 -51
- data/spec/unit/operations/each_spec.rb +0 -49
- data/spec/unit/operations/implication_spec.rb +0 -32
- data/spec/unit/operations/key_spec.rb +0 -135
- data/spec/unit/operations/negation_spec.rb +0 -51
- data/spec/unit/operations/or_spec.rb +0 -75
- data/spec/unit/operations/set_spec.rb +0 -43
- data/spec/unit/operations/xor_spec.rb +0 -63
- data/spec/unit/predicates/array_spec.rb +0 -43
- data/spec/unit/predicates/attr_spec.rb +0 -31
- data/spec/unit/predicates/bool_spec.rb +0 -36
- data/spec/unit/predicates/case_spec.rb +0 -35
- data/spec/unit/predicates/date_spec.rb +0 -33
- data/spec/unit/predicates/date_time_spec.rb +0 -33
- data/spec/unit/predicates/decimal_spec.rb +0 -34
- data/spec/unit/predicates/empty_spec.rb +0 -40
- data/spec/unit/predicates/eql_spec.rb +0 -23
- data/spec/unit/predicates/even_spec.rb +0 -33
- data/spec/unit/predicates/excluded_from_spec.rb +0 -37
- data/spec/unit/predicates/excludes_spec.rb +0 -58
- data/spec/unit/predicates/false_spec.rb +0 -37
- data/spec/unit/predicates/filled_spec.rb +0 -40
- data/spec/unit/predicates/float_spec.rb +0 -33
- data/spec/unit/predicates/format_spec.rb +0 -23
- data/spec/unit/predicates/gt_spec.rb +0 -42
- data/spec/unit/predicates/gteq_spec.rb +0 -42
- data/spec/unit/predicates/included_in_spec.rb +0 -37
- data/spec/unit/predicates/includes_spec.rb +0 -24
- data/spec/unit/predicates/int_spec.rb +0 -36
- data/spec/unit/predicates/key_spec.rb +0 -31
- data/spec/unit/predicates/lt_spec.rb +0 -42
- data/spec/unit/predicates/lteq_spec.rb +0 -42
- data/spec/unit/predicates/max_size_spec.rb +0 -51
- data/spec/unit/predicates/min_size_spec.rb +0 -51
- data/spec/unit/predicates/none_spec.rb +0 -30
- data/spec/unit/predicates/not_eql_spec.rb +0 -23
- data/spec/unit/predicates/number_spec.rb +0 -39
- data/spec/unit/predicates/odd_spec.rb +0 -33
- data/spec/unit/predicates/respond_to_spec.rb +0 -31
- data/spec/unit/predicates/size_spec.rb +0 -57
- data/spec/unit/predicates/str_spec.rb +0 -34
- data/spec/unit/predicates/time_spec.rb +0 -33
- data/spec/unit/predicates/true_spec.rb +0 -37
- data/spec/unit/predicates/type_spec.rb +0 -37
- data/spec/unit/predicates/uuid_v4_spec.rb +0 -29
- data/spec/unit/predicates_spec.rb +0 -25
- data/spec/unit/rule/predicate_spec.rb +0 -55
- data/spec/unit/rule_compiler_spec.rb +0 -129
- data/spec/unit/rule_spec.rb +0 -213
@@ -1,30 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#nil?' do
|
7
|
-
let(:predicate_name) { :nil? }
|
8
|
-
|
9
|
-
context 'when value is nil' do
|
10
|
-
let(:arguments_list) { [[nil]] }
|
11
|
-
it_behaves_like 'a passing predicate'
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'when value is not nil' do
|
15
|
-
let(:arguments_list) do
|
16
|
-
[
|
17
|
-
[''],
|
18
|
-
[true],
|
19
|
-
[false],
|
20
|
-
[0],
|
21
|
-
[:symbol],
|
22
|
-
[[]],
|
23
|
-
[{}],
|
24
|
-
[String]
|
25
|
-
]
|
26
|
-
end
|
27
|
-
it_behaves_like 'a failing predicate'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates, '#not_eql?' do
|
6
|
-
let(:predicate_name) { :not_eql? }
|
7
|
-
|
8
|
-
context 'when value is equal to the arg' do
|
9
|
-
let(:arguments_list) do
|
10
|
-
[['Foo', 'Foo']]
|
11
|
-
end
|
12
|
-
|
13
|
-
it_behaves_like 'a failing predicate'
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'with value is not equal to the arg' do
|
17
|
-
let(:arguments_list) do
|
18
|
-
[['Bar', 'Foo']]
|
19
|
-
end
|
20
|
-
|
21
|
-
it_behaves_like 'a passing predicate'
|
22
|
-
end
|
23
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#number?' do
|
7
|
-
let(:predicate_name) { :number? }
|
8
|
-
|
9
|
-
context 'when value is numerical' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[
|
12
|
-
["34"],
|
13
|
-
["1.000004"],
|
14
|
-
["0"],
|
15
|
-
[4],
|
16
|
-
["-15.24"],
|
17
|
-
[-3.5]
|
18
|
-
]
|
19
|
-
end
|
20
|
-
|
21
|
-
it_behaves_like 'a passing predicate'
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'with value is not numerical' do
|
25
|
-
let(:arguments_list) do
|
26
|
-
[
|
27
|
-
[''],
|
28
|
-
["-14px"],
|
29
|
-
["10,150.00"],
|
30
|
-
[nil],
|
31
|
-
[:symbol],
|
32
|
-
[String]
|
33
|
-
]
|
34
|
-
end
|
35
|
-
|
36
|
-
it_behaves_like 'a failing predicate'
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#odd?' do
|
7
|
-
let(:predicate_name) { :odd? }
|
8
|
-
|
9
|
-
context 'when value is an odd int' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[
|
12
|
-
[13],
|
13
|
-
[1],
|
14
|
-
[1111]
|
15
|
-
]
|
16
|
-
end
|
17
|
-
|
18
|
-
it_behaves_like 'a passing predicate'
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'with value is an even int' do
|
22
|
-
let(:arguments_list) do
|
23
|
-
[
|
24
|
-
[0],
|
25
|
-
[2],
|
26
|
-
[2222]
|
27
|
-
]
|
28
|
-
end
|
29
|
-
|
30
|
-
it_behaves_like 'a failing predicate'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#respond_to?' do
|
7
|
-
let(:predicate_name) { :respond_to? }
|
8
|
-
|
9
|
-
context 'when value responds to method' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[
|
12
|
-
[:method, Object],
|
13
|
-
[:new, Hash]
|
14
|
-
]
|
15
|
-
end
|
16
|
-
|
17
|
-
it_behaves_like 'a passing predicate'
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'when value does not respond to method' do
|
21
|
-
let(:arguments_list) do
|
22
|
-
[
|
23
|
-
[:foo, Object],
|
24
|
-
[:bar, Hash]
|
25
|
-
]
|
26
|
-
end
|
27
|
-
|
28
|
-
it_behaves_like 'a failing predicate'
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#size?' do
|
7
|
-
let(:predicate_name) { :size? }
|
8
|
-
|
9
|
-
context 'when value size is equal to n' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[
|
12
|
-
[[8], 2],
|
13
|
-
[4, 'Jill'],
|
14
|
-
[2, { 1 => 'st', 2 => 'nd' }],
|
15
|
-
[8, 8],
|
16
|
-
[1..8, 5]
|
17
|
-
]
|
18
|
-
end
|
19
|
-
|
20
|
-
it_behaves_like 'a passing predicate'
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'when value size is greater than n' do
|
24
|
-
let(:arguments_list) do
|
25
|
-
[
|
26
|
-
[[1, 2], 3],
|
27
|
-
[5, 'Jill'],
|
28
|
-
[3, { 1 => 'st', 2 => 'nd' }],
|
29
|
-
[1, 9],
|
30
|
-
[1..5, 6]
|
31
|
-
]
|
32
|
-
end
|
33
|
-
|
34
|
-
it_behaves_like 'a failing predicate'
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'with value size is less than n' do
|
38
|
-
let(:arguments_list) do
|
39
|
-
[
|
40
|
-
[[1, 2], 1],
|
41
|
-
[3, 'Jill'],
|
42
|
-
[1, { 1 => 'st', 2 => 'nd' }],
|
43
|
-
[1, 7],
|
44
|
-
[1..5, 4]
|
45
|
-
]
|
46
|
-
end
|
47
|
-
|
48
|
-
it_behaves_like 'a failing predicate'
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'with an unsupported size' do
|
52
|
-
it 'raises an error' do
|
53
|
-
expect { Predicates[:size?].call('oops', 1) }.to raise_error(ArgumentError, /oops/)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#str?' do
|
7
|
-
let(:predicate_name) { :str? }
|
8
|
-
|
9
|
-
context 'when value is a string' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[
|
12
|
-
[''],
|
13
|
-
['John']
|
14
|
-
]
|
15
|
-
end
|
16
|
-
|
17
|
-
it_behaves_like 'a passing predicate'
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with value is not a string' do
|
21
|
-
let(:arguments_list) do
|
22
|
-
[
|
23
|
-
[[]],
|
24
|
-
[{}],
|
25
|
-
[nil],
|
26
|
-
[:symbol],
|
27
|
-
[String]
|
28
|
-
]
|
29
|
-
end
|
30
|
-
|
31
|
-
it_behaves_like 'a failing predicate'
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#time?' do
|
7
|
-
let(:predicate_name) { :time? }
|
8
|
-
|
9
|
-
context 'when value is a time' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[[Time.now]]
|
12
|
-
end
|
13
|
-
|
14
|
-
it_behaves_like 'a passing predicate'
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'with value is not an integer' do
|
18
|
-
let(:arguments_list) do
|
19
|
-
[
|
20
|
-
[''],
|
21
|
-
[[]],
|
22
|
-
[{}],
|
23
|
-
[nil],
|
24
|
-
[:symbol],
|
25
|
-
[String],
|
26
|
-
[1]
|
27
|
-
]
|
28
|
-
end
|
29
|
-
|
30
|
-
it_behaves_like 'a failing predicate'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#true?' do
|
7
|
-
let(:predicate_name) { :true? }
|
8
|
-
|
9
|
-
context 'when value is true' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[[true]]
|
12
|
-
end
|
13
|
-
|
14
|
-
it_behaves_like 'a passing predicate'
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'with value is not true' do
|
18
|
-
let(:arguments_list) do
|
19
|
-
[
|
20
|
-
[false],
|
21
|
-
[''],
|
22
|
-
[[]],
|
23
|
-
[{}],
|
24
|
-
[nil],
|
25
|
-
[:symbol],
|
26
|
-
[String],
|
27
|
-
[1],
|
28
|
-
[0],
|
29
|
-
['true'],
|
30
|
-
['false']
|
31
|
-
]
|
32
|
-
end
|
33
|
-
|
34
|
-
it_behaves_like 'a failing predicate'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#type?' do
|
7
|
-
let(:predicate_name) { :type? }
|
8
|
-
|
9
|
-
context 'when value has a correct type' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[[TrueClass, true]]
|
12
|
-
end
|
13
|
-
|
14
|
-
it_behaves_like 'a passing predicate'
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'with value is not true' do
|
18
|
-
let(:arguments_list) do
|
19
|
-
[
|
20
|
-
[TrueClass, false],
|
21
|
-
[TrueClass, ''],
|
22
|
-
[TrueClass, []],
|
23
|
-
[TrueClass, {}],
|
24
|
-
[TrueClass, nil],
|
25
|
-
[TrueClass, :symbol],
|
26
|
-
[TrueClass, String],
|
27
|
-
[TrueClass, 1],
|
28
|
-
[TrueClass, 0],
|
29
|
-
[TrueClass, 'true'],
|
30
|
-
[TrueClass, 'false']
|
31
|
-
]
|
32
|
-
end
|
33
|
-
|
34
|
-
it_behaves_like 'a failing predicate'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Dry::Logic::Predicates do
|
6
|
-
describe '#uuid_v4?' do
|
7
|
-
let(:predicate_name) { :uuid_v4? }
|
8
|
-
|
9
|
-
context 'when value is a valid V4 UUID' do
|
10
|
-
let(:arguments_list) do
|
11
|
-
[['f2d26c57-e07c-4416-a749-57e937930e04']]
|
12
|
-
end
|
13
|
-
|
14
|
-
it_behaves_like 'a passing predicate'
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'with value is not a valid V4 UUID' do
|
18
|
-
let(:arguments_list) do
|
19
|
-
[
|
20
|
-
['f2d26c57-e07c-3416-a749-57e937930e04'], # wrong version number (3, not 4)
|
21
|
-
['20633928-6a07-11e9-a923-1681be663d3e'], # UUID V1
|
22
|
-
['not-a-uuid-at-all']
|
23
|
-
]
|
24
|
-
end
|
25
|
-
|
26
|
-
it_behaves_like 'a failing predicate'
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'dry/logic/predicates'
|
4
|
-
|
5
|
-
RSpec.describe Predicates do
|
6
|
-
it 'can be included in another module' do
|
7
|
-
mod = Module.new { include Predicates }
|
8
|
-
|
9
|
-
expect(mod[:key?]).to be_a(Method)
|
10
|
-
end
|
11
|
-
|
12
|
-
describe '.predicate' do
|
13
|
-
it 'defines a predicate method' do
|
14
|
-
mod = Module.new {
|
15
|
-
include Predicates
|
16
|
-
|
17
|
-
predicate(:test?) do |foo|
|
18
|
-
true
|
19
|
-
end
|
20
|
-
}
|
21
|
-
|
22
|
-
expect(mod.test?('arg')).to be(true)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Rule::Predicate do
|
4
|
-
subject(:rule) { Rule::Predicate.build(predicate) }
|
5
|
-
|
6
|
-
let(:predicate) { str? }
|
7
|
-
|
8
|
-
include_context 'predicates'
|
9
|
-
|
10
|
-
it_behaves_like Rule
|
11
|
-
|
12
|
-
describe '#name' do
|
13
|
-
it 'returns predicate identifier' do
|
14
|
-
expect(rule.name).to be(:str?)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '#to_ast' do
|
19
|
-
context 'without a result' do
|
20
|
-
it 'returns rule ast' do
|
21
|
-
expect(rule.to_ast).to eql([:predicate, [:str?, [[:input, Undefined]]]])
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'returns :failure with an id' do
|
25
|
-
email = rule.with(id: :email)
|
26
|
-
|
27
|
-
expect(email.(11).to_ast).to eql([:failure, [:email, [:predicate, [:str?, [[:input, 11]]]]]])
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context 'with a result' do
|
32
|
-
it 'returns success' do
|
33
|
-
expect(rule.('foo')).to be_success
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'returns failure ast' do
|
37
|
-
expect(rule.(5).to_ast).to eql([:predicate, [:str?, [[:input, 5]]]])
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'with a zero-arity predicate' do
|
42
|
-
let(:predicate) { Module.new { def self.test?; true; end }.method(:test?) }
|
43
|
-
|
44
|
-
it 'returns ast' do
|
45
|
-
expect(rule.to_ast).to eql([:predicate, [:test?, []]])
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe '#to_s' do
|
51
|
-
it 'returns string representation' do
|
52
|
-
expect(rule.curry('foo').to_s).to eql('str?("foo")')
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|