dry-logic 0.5.0 → 1.5.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +222 -17
- data/LICENSE +1 -1
- data/README.md +11 -20
- data/dry-logic.gemspec +36 -20
- data/lib/dry/logic/appliable.rb +2 -0
- data/lib/dry/logic/builder.rb +88 -0
- data/lib/dry/logic/evaluator.rb +1 -1
- data/lib/dry/logic/operations/abstract.rb +4 -6
- data/lib/dry/logic/operations/and.rb +12 -3
- data/lib/dry/logic/operations/attr.rb +1 -1
- data/lib/dry/logic/operations/binary.rb +4 -3
- data/lib/dry/logic/operations/check.rb +3 -5
- data/lib/dry/logic/operations/each.rb +1 -2
- data/lib/dry/logic/operations/implication.rb +1 -2
- data/lib/dry/logic/operations/key.rb +3 -5
- data/lib/dry/logic/operations/negation.rb +1 -2
- data/lib/dry/logic/operations/or.rb +1 -2
- data/lib/dry/logic/operations/set.rb +3 -4
- data/lib/dry/logic/operations/unary.rb +1 -1
- data/lib/dry/logic/operations/xor.rb +1 -2
- data/lib/dry/logic/operators.rb +6 -4
- data/lib/dry/logic/predicates.rb +110 -28
- data/lib/dry/logic/result.rb +2 -4
- data/lib/dry/logic/rule/interface.rb +143 -0
- data/lib/dry/logic/rule/predicate.rb +23 -17
- data/lib/dry/logic/rule.rb +31 -31
- data/lib/dry/logic/rule_compiler.rb +2 -7
- data/lib/dry/logic/version.rb +3 -1
- data/lib/dry/logic.rb +21 -5
- data/lib/dry-logic.rb +3 -1
- metadata +27 -143
- data/.codeclimate.yml +0 -23
- data/.gitignore +0 -7
- data/.rspec +0 -3
- data/.rubocop.yml +0 -16
- data/.rubocop_todo.yml +0 -7
- data/.travis.yml +0 -30
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -17
- data/Rakefile +0 -12
- data/bin/console +0 -10
- data/examples/basic.rb +0 -14
- data/lib/dry/logic/operations.rb +0 -13
- data/spec/integration/result_spec.rb +0 -59
- data/spec/integration/rule_spec.rb +0 -53
- data/spec/shared/predicates.rb +0 -57
- data/spec/shared/rule.rb +0 -67
- data/spec/spec_helper.rb +0 -34
- data/spec/support/mutant.rb +0 -9
- data/spec/unit/operations/and_spec.rb +0 -64
- data/spec/unit/operations/attr_spec.rb +0 -27
- data/spec/unit/operations/check_spec.rb +0 -49
- data/spec/unit/operations/each_spec.rb +0 -47
- data/spec/unit/operations/implication_spec.rb +0 -30
- data/spec/unit/operations/key_spec.rb +0 -133
- data/spec/unit/operations/negation_spec.rb +0 -49
- data/spec/unit/operations/or_spec.rb +0 -73
- data/spec/unit/operations/set_spec.rb +0 -41
- data/spec/unit/operations/xor_spec.rb +0 -61
- data/spec/unit/predicates/array_spec.rb +0 -41
- data/spec/unit/predicates/attr_spec.rb +0 -29
- data/spec/unit/predicates/bool_spec.rb +0 -34
- data/spec/unit/predicates/case_spec.rb +0 -33
- data/spec/unit/predicates/date_spec.rb +0 -31
- data/spec/unit/predicates/date_time_spec.rb +0 -31
- data/spec/unit/predicates/decimal_spec.rb +0 -32
- data/spec/unit/predicates/empty_spec.rb +0 -38
- data/spec/unit/predicates/eql_spec.rb +0 -21
- data/spec/unit/predicates/even_spec.rb +0 -31
- data/spec/unit/predicates/excluded_from_spec.rb +0 -35
- data/spec/unit/predicates/excludes_spec.rb +0 -56
- data/spec/unit/predicates/false_spec.rb +0 -35
- data/spec/unit/predicates/filled_spec.rb +0 -38
- data/spec/unit/predicates/float_spec.rb +0 -31
- data/spec/unit/predicates/format_spec.rb +0 -21
- data/spec/unit/predicates/gt_spec.rb +0 -40
- data/spec/unit/predicates/gteq_spec.rb +0 -40
- data/spec/unit/predicates/included_in_spec.rb +0 -35
- data/spec/unit/predicates/includes_spec.rb +0 -24
- data/spec/unit/predicates/int_spec.rb +0 -34
- data/spec/unit/predicates/key_spec.rb +0 -29
- data/spec/unit/predicates/lt_spec.rb +0 -40
- data/spec/unit/predicates/lteq_spec.rb +0 -40
- data/spec/unit/predicates/max_size_spec.rb +0 -49
- data/spec/unit/predicates/min_size_spec.rb +0 -49
- data/spec/unit/predicates/none_spec.rb +0 -28
- data/spec/unit/predicates/not_eql_spec.rb +0 -21
- data/spec/unit/predicates/number_spec.rb +0 -37
- data/spec/unit/predicates/odd_spec.rb +0 -31
- data/spec/unit/predicates/size_spec.rb +0 -55
- data/spec/unit/predicates/str_spec.rb +0 -32
- data/spec/unit/predicates/time_spec.rb +0 -31
- data/spec/unit/predicates/true_spec.rb +0 -35
- data/spec/unit/predicates/type_spec.rb +0 -35
- data/spec/unit/predicates_spec.rb +0 -23
- data/spec/unit/rule/predicate_spec.rb +0 -53
- data/spec/unit/rule_compiler_spec.rb +0 -127
- data/spec/unit/rule_spec.rb +0 -141
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58ce7aca14ea687be3852f8f6359ea33de4da0f057091127f828dbb2e6db8856
|
4
|
+
data.tar.gz: 283cf7ff61520f4ca1822bea434bd9b98ccb07c10e17f628e0f440029ca76092
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d16672e062a05eb7c79a5ce34f47307b8eb7cba7cefce4c39d513f835b59fd711968e5bccb8a5f6e7c125aeec6485a4ff9193cc046390c44e2f4f4090f3ad44
|
7
|
+
data.tar.gz: 4fbe122584b52eda9d699726023c09fcc9e636f8f67d3670de584b0a7d6345ebdac17ea7173bfa753aab35500eeb8f95d9941b851c980cd6fcac5c3cac8d879d
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,189 @@
|
|
1
|
-
|
1
|
+
<!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
|
2
|
+
|
3
|
+
## 1.5.0 2022-11-24
|
4
|
+
|
5
|
+
|
6
|
+
### Added
|
7
|
+
|
8
|
+
- `uri_rfc3986?` predicate that uses a better regexp than `uri?` (see #94 for more details) (@hieuk09)
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- Made `Predicates.respond_to?` compatible with `Object#respond_to?` (via #105) (@solnic)
|
13
|
+
- Made `Predicates.eql?` compatible with `Object#eql?` (via #106) (@solnic)
|
14
|
+
|
15
|
+
[Compare v1.4.0...v1.5.0](https://github.com/dry-rb/dry-logic/compare/v1.4.0...v1.5.0)
|
16
|
+
|
17
|
+
## 1.4.0 2022-11-04
|
18
|
+
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
|
22
|
+
- Updated to dry-core 1.0 (@flash-gordon + @solnic)
|
23
|
+
|
24
|
+
[Compare v1.3.0...v1.4.0](https://github.com/dry-rb/dry-logic/compare/v1.3.0...v1.4.0)
|
25
|
+
|
26
|
+
## 1.3.0 2022-10-15
|
27
|
+
|
28
|
+
|
29
|
+
### Changed
|
30
|
+
|
31
|
+
- Use zeitwerk for auto-loading (@solnic + @flash-gordon)
|
32
|
+
|
33
|
+
[Compare v1.2.0...v1.3.0](https://github.com/dry-rb/dry-logic/compare/v1.2.0...v1.3.0)
|
34
|
+
|
35
|
+
## 1.2.0 2021-04-26
|
36
|
+
|
37
|
+
|
38
|
+
### Added
|
39
|
+
|
40
|
+
- Add predicate and operation builder DSL (@oleander)
|
41
|
+
|
42
|
+
|
43
|
+
[Compare v1.1.1...v1.2.0](https://github.com/dry-rb/dry-logic/compare/v1.1.1...v1.2.0)
|
44
|
+
|
45
|
+
## 1.1.1 2021-04-14
|
46
|
+
|
47
|
+
|
48
|
+
### Fixed
|
49
|
+
|
50
|
+
- Fixed a crash under jruby caused by arg splatting in Binary operations (@flash-gordon)
|
51
|
+
|
52
|
+
|
53
|
+
[Compare v1.1.0...v1.1.1](https://github.com/dry-rb/dry-logic/compare/v1.1.0...v1.1.1)
|
54
|
+
|
55
|
+
## 1.1.0 2020-12-26
|
56
|
+
|
57
|
+
|
58
|
+
### Fixed
|
59
|
+
|
60
|
+
- Nested `Check` operations no longer crash under MRI 3.0 (@oleander)
|
61
|
+
|
62
|
+
### Changed
|
63
|
+
|
64
|
+
- Switched to equalizer from dry-core (@solnic)
|
65
|
+
|
66
|
+
[Compare v1.0.8...v1.1.0](https://github.com/dry-rb/dry-logic/compare/v1.0.8...v1.1.0)
|
67
|
+
|
68
|
+
## 1.0.8 2020-09-28
|
69
|
+
|
70
|
+
|
71
|
+
### Fixed
|
72
|
+
|
73
|
+
- Better Ruby 3 support with fixed specialization for rules of negative arity (@flash-gordon)
|
74
|
+
|
75
|
+
|
76
|
+
[Compare v1.0.7...v1.0.8](https://github.com/dry-rb/dry-logic/compare/v1.0.7...v1.0.8)
|
77
|
+
|
78
|
+
## 1.0.7 2020-08-13
|
79
|
+
|
80
|
+
|
81
|
+
### Added
|
82
|
+
|
83
|
+
- A new `uri?` predicate that you can use to verify `URI` strings, ie `uri?("https", "https://dry-rb.org")` (@nerburish)
|
84
|
+
- New predicates: `uuid_v1?`, `uuid_v2?`, `uuid_v3?` and `uuid_v5?` (via #75) (@jamesbrauman)
|
85
|
+
|
86
|
+
|
87
|
+
[Compare v1.0.6...v1.0.7](https://github.com/dry-rb/dry-logic/compare/v1.0.6...v1.0.7)
|
88
|
+
|
89
|
+
## 1.0.6 2020-02-10
|
90
|
+
|
91
|
+
|
92
|
+
### Fixed
|
93
|
+
|
94
|
+
- Made the regexp used by `uuid_v4?` more secure (@kml)
|
95
|
+
|
96
|
+
|
97
|
+
[Compare v1.0.5...v1.0.6](https://github.com/dry-rb/dry-logic/compare/v1.0.5...v1.0.6)
|
98
|
+
|
99
|
+
## 1.0.5 2019-11-07
|
100
|
+
|
101
|
+
|
102
|
+
### Fixed
|
103
|
+
|
104
|
+
- Make `format?` tolerant to `nil` values. It already worked like that before, but starting Ruby 2.7 it would produce warnings. Now it won't. Don't rely on this behavior, it's only added to make tests pass in dry-schema. Use explicit type checks instead (@flash-gordon)
|
105
|
+
|
106
|
+
|
107
|
+
[Compare v1.0.4...v1.0.5](https://github.com/dry-rb/dry-logic/compare/v1.0.4...v1.0.5)
|
108
|
+
|
109
|
+
## 1.0.4 2019-11-06
|
110
|
+
|
111
|
+
|
112
|
+
### Fixed
|
113
|
+
|
114
|
+
- Fix keyword warnings (@flash-gordon)
|
115
|
+
|
116
|
+
|
117
|
+
[Compare v1.0.3...v1.0.4](https://github.com/dry-rb/dry-logic/compare/v1.0.3...v1.0.4)
|
118
|
+
|
119
|
+
## 1.0.3 2019-08-01
|
120
|
+
|
121
|
+
|
122
|
+
### Added
|
123
|
+
|
124
|
+
- `bytesize?` predicate (@bmalinconico)
|
125
|
+
- `min_bytesize?` predicate (@bmalinconico)
|
126
|
+
- `max_bytesize? predicate (@bmalinconico)
|
127
|
+
|
128
|
+
### Changed
|
129
|
+
|
130
|
+
- Min ruby version was set to `>= 2.4.0` (@flash-gordon)
|
131
|
+
|
132
|
+
[Compare v1.0.2...v1.0.3](https://github.com/dry-rb/dry-logic/compare/v1.0.2...v1.0.3)
|
133
|
+
|
134
|
+
## 1.0.2 2019-06-14
|
135
|
+
|
136
|
+
Re-pushed 1.0.1 after dry-schema 1.2.0 release.
|
137
|
+
|
138
|
+
|
139
|
+
[Compare v1.0.1...v1.0.2](https://github.com/dry-rb/dry-logic/compare/v1.0.1...v1.0.2)
|
140
|
+
|
141
|
+
## 1.0.1 2019-06-04
|
142
|
+
|
143
|
+
This release was removed from rubygems because it broke dry-schema.
|
144
|
+
|
145
|
+
### Added
|
146
|
+
|
147
|
+
- `uuid_v4?` predicate (radar)
|
148
|
+
- `respond_to?` predicate (waiting-for-dev)
|
149
|
+
|
150
|
+
|
151
|
+
[Compare v1.0.0...v1.0.1](https://github.com/dry-rb/dry-logic/compare/v1.0.0...v1.0.1)
|
152
|
+
|
153
|
+
## 1.0.0 2019-04-23
|
154
|
+
|
155
|
+
|
156
|
+
### Changed
|
157
|
+
|
158
|
+
- Version bump to `1.0.0` (flash-gordon)
|
159
|
+
|
160
|
+
[Compare v0.6.1...v1.0.0](https://github.com/dry-rb/dry-logic/compare/v0.6.1...v1.0.0)
|
161
|
+
|
162
|
+
## 0.6.1 2019-04-18
|
163
|
+
|
164
|
+
|
165
|
+
### Fixed
|
166
|
+
|
167
|
+
- Fix a regression in dry-validation 0.x for argument-less predicates (flash-gordon)
|
168
|
+
|
169
|
+
|
170
|
+
[Compare v0.6.0...v0.6.1](https://github.com/dry-rb/dry-logic/compare/v0.6.0...v0.6.1)
|
171
|
+
|
172
|
+
## 0.6.0 2019-04-04
|
173
|
+
|
174
|
+
|
175
|
+
### Added
|
176
|
+
|
177
|
+
- Generating hints can be disabled by building `Operations::And` with `hints: false` option set (solnic)
|
178
|
+
|
179
|
+
### Changed
|
180
|
+
|
181
|
+
- `Rule` construction has been optimized so that currying and application is multiple-times faster (flash-gordon)
|
182
|
+
|
183
|
+
[Compare v0.5.0...v0.6.0](https://github.com/dry-rb/dry-logic/compare/v0.5.0...v0.6.0)
|
184
|
+
|
185
|
+
## 0.5.0 2019-01-29
|
186
|
+
|
2
187
|
|
3
188
|
### Added
|
4
189
|
|
@@ -8,9 +193,11 @@
|
|
8
193
|
|
9
194
|
- `Operation::Key#ast` will now return a correct AST with non-Undefined inputs (solnic)
|
10
195
|
|
196
|
+
|
11
197
|
[Compare v0.4.2...v0.5.0](https://github.com/dry-rb/dry-logic/compare/v0.4.2...v0.5.0)
|
12
198
|
|
13
|
-
|
199
|
+
## 0.4.2 2017-09-15
|
200
|
+
|
14
201
|
|
15
202
|
### Added
|
16
203
|
|
@@ -21,21 +208,23 @@
|
|
21
208
|
|
22
209
|
- A bug with using custom predicates within a standalone module in `dry-validation` (flash-gordon)
|
23
210
|
|
24
|
-
[Compare v0.4.1...v0.4.2](https://github.com/dry-rb/dry-logic/compare/v0.4.1...v0.4.2)
|
25
211
|
|
26
|
-
|
212
|
+
[Compare v0.4.1...v0.4.2](https://github.com/dry-rb/dry-logic/compare/v0.4.1...v0.4.2)
|
27
213
|
|
28
|
-
|
214
|
+
## 0.4.1 2017-01-23
|
29
215
|
|
30
|
-
- Predicates simply reuse other predicate methods instead of referring to them via `#[]` (georgemillo)
|
31
216
|
|
32
217
|
### Fixed
|
33
218
|
|
34
219
|
- Warnings on MRI 2.4.0 are gone (jtippett)
|
35
220
|
|
221
|
+
### Changed
|
222
|
+
|
223
|
+
- Predicates simply reuse other predicate methods instead of referring to them via `#[]` (georgemillo)
|
224
|
+
|
36
225
|
[Compare v0.4.0...v0.4.1](https://github.com/dry-rb/dry-logic/compare/v0.4.0...v0.4.1)
|
37
226
|
|
38
|
-
|
227
|
+
## 0.4.0 2016-09-21
|
39
228
|
|
40
229
|
This is a partial rewrite focused on internal clean up and major performance improvements. This is also the beginning of the work to make this library first-class rather than "just" a rule backend for dry-validation and dry-types.
|
41
230
|
|
@@ -63,7 +252,8 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
63
252
|
|
64
253
|
[Compare v0.3.0...v0.4.0](https://github.com/dry-rb/dry-logic/compare/v0.3.0...v0.4.0)
|
65
254
|
|
66
|
-
|
255
|
+
## 0.3.0 2016-07-01
|
256
|
+
|
67
257
|
|
68
258
|
### Added
|
69
259
|
|
@@ -78,7 +268,8 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
78
268
|
|
79
269
|
[Compare v0.2.3...v0.3.0](https://github.com/dry-rb/dry-logic/compare/v0.2.3...v0.3.0)
|
80
270
|
|
81
|
-
|
271
|
+
## 0.2.3 2016-05-11
|
272
|
+
|
82
273
|
|
83
274
|
### Added
|
84
275
|
|
@@ -91,21 +282,28 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
91
282
|
|
92
283
|
[Compare v0.2.2...v0.2.3](https://github.com/dry-rb/dry-logic/compare/v0.2.2...v0.2.3)
|
93
284
|
|
94
|
-
|
285
|
+
## 0.2.2 2016-03-30
|
286
|
+
|
95
287
|
|
96
288
|
### Added
|
97
289
|
|
98
290
|
- `number?`, `odd?`, `even?` predicates (fran-worley)
|
99
291
|
|
292
|
+
|
100
293
|
[Compare v0.2.1...v0.2.2](https://github.com/dry-rb/dry-logic/compare/v0.2.1...v0.2.2)
|
101
294
|
|
102
|
-
|
295
|
+
## 0.2.1 2016-03-20
|
296
|
+
|
103
297
|
|
104
298
|
### Fixed
|
105
299
|
|
106
300
|
- Result AST for `Rule::Each` correctly maps elements with eql inputs (solnic)
|
107
301
|
|
108
|
-
|
302
|
+
|
303
|
+
[Compare v0.2.0...v0.2.1](https://github.com/dry-rb/dry-logic/compare/v0.2.0...v0.2.1)
|
304
|
+
|
305
|
+
## 0.2.0 2016-03-11
|
306
|
+
|
109
307
|
|
110
308
|
### Changed
|
111
309
|
|
@@ -113,16 +311,19 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
113
311
|
|
114
312
|
[Compare v0.1.4...v0.2.0](https://github.com/dry-rb/dry-logic/compare/v0.1.4...v0.2.0)
|
115
313
|
|
116
|
-
|
314
|
+
## 0.1.4 2016-01-27
|
315
|
+
|
117
316
|
|
118
317
|
### Added
|
119
318
|
|
120
319
|
- Support for hash-names in `Check` and `Result` which can properly resolve input
|
121
320
|
from nested results (solnic)
|
122
321
|
|
322
|
+
|
123
323
|
[Compare v0.1.3...v0.1.4](https://github.com/dry-rb/dry-logic/compare/v0.1.3...v0.1.4)
|
124
324
|
|
125
|
-
|
325
|
+
## 0.1.3 2016-01-27
|
326
|
+
|
126
327
|
|
127
328
|
### Added
|
128
329
|
|
@@ -134,15 +335,18 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
134
335
|
|
135
336
|
[Compare v0.1.2...v0.1.3](https://github.com/dry-rb/dry-logic/compare/v0.1.2...v0.1.3)
|
136
337
|
|
137
|
-
|
338
|
+
## 0.1.2 2016-01-19
|
339
|
+
|
138
340
|
|
139
341
|
### Fixed
|
140
342
|
|
141
343
|
- `xor` returns wrapped results when used against another result-rule (solnic)
|
142
344
|
|
345
|
+
|
143
346
|
[Compare v0.1.1...v0.1.2](https://github.com/dry-rb/dry-logic/compare/v0.1.1...v0.1.2)
|
144
347
|
|
145
|
-
|
348
|
+
## 0.1.1 2016-01-18
|
349
|
+
|
146
350
|
|
147
351
|
### Added
|
148
352
|
|
@@ -150,8 +354,9 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
150
354
|
- `Rule::Result` which can be applied to a result object (solnic)
|
151
355
|
- `true?` and `false?` predicates (solnic)
|
152
356
|
|
357
|
+
|
153
358
|
[Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-logic/compare/v0.1.0...v0.1.1)
|
154
359
|
|
155
|
-
|
360
|
+
## 0.1.0 2016-01-11
|
156
361
|
|
157
362
|
Code extracted from dry-validation 0.4.1
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,30 +1,21 @@
|
|
1
|
+
<!--- this file is synced from dry-rb/template-gem project -->
|
1
2
|
[gem]: https://rubygems.org/gems/dry-logic
|
2
|
-
[
|
3
|
-
[codeclimate]: https://codeclimate.com/github/dry-rb/dry-logic
|
4
|
-
[coveralls]: https://coveralls.io/r/dry-rb/dry-logic
|
5
|
-
[inchpages]: http://inch-ci.org/github/dry-rb/dry-logic
|
3
|
+
[actions]: https://github.com/dry-rb/dry-logic/actions
|
6
4
|
|
7
|
-
# dry-logic [][gem]
|
10
|
-
[][travis]
|
11
|
-
[][codeclimate]
|
12
|
-
[][codeclimate]
|
13
|
-
[][inchpages]
|
14
|
-
|
15
|
-
Predicate logic and rule composition used by:
|
16
|
-
|
17
|
-
* [dry-types](https://github.com/dry-rb/dry-types) for constrained types
|
18
|
-
* [dry-validation](https://github.com/dry-rb/dry-validation) for composing validation rules
|
19
|
-
* your project...?
|
5
|
+
# dry-logic [][gem] [][actions]
|
20
6
|
|
21
7
|
## Links
|
22
8
|
|
23
|
-
* [
|
9
|
+
* [User documentation](https://dry-rb.org/gems/dry-logic)
|
10
|
+
* [API documentation](http://rubydoc.info/gems/dry-logic)
|
11
|
+
* [Forum](https://discourse.dry-rb.org)
|
12
|
+
|
13
|
+
## Supported Ruby versions
|
24
14
|
|
25
|
-
|
15
|
+
This library officially supports the following Ruby versions:
|
26
16
|
|
27
|
-
|
17
|
+
* MRI `>= 2.7.0`
|
18
|
+
* jruby `>= 9.3` (postponed until 2.7 is supported)
|
28
19
|
|
29
20
|
## License
|
30
21
|
|
data/dry-logic.gemspec
CHANGED
@@ -1,23 +1,39 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# this file is synced from dry-rb/template-gem project
|
4
|
+
|
5
|
+
lib = File.expand_path("lib", __dir__)
|
6
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
7
|
+
require "dry/logic/version"
|
2
8
|
|
3
9
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name =
|
5
|
-
spec.
|
6
|
-
spec.
|
7
|
-
spec.
|
8
|
-
spec.
|
9
|
-
|
10
|
-
spec.
|
11
|
-
|
12
|
-
spec.
|
13
|
-
spec.
|
14
|
-
spec.
|
15
|
-
|
16
|
-
spec.
|
17
|
-
|
18
|
-
spec.
|
19
|
-
|
20
|
-
spec.
|
21
|
-
spec.
|
22
|
-
|
10
|
+
spec.name = "dry-logic"
|
11
|
+
spec.authors = ["Piotr Solnica"]
|
12
|
+
spec.email = ["piotr.solnica@gmail.com"]
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.version = Dry::Logic::VERSION.dup
|
15
|
+
|
16
|
+
spec.summary = "Predicate logic with rule composition"
|
17
|
+
spec.description = spec.summary
|
18
|
+
spec.homepage = "https://dry-rb.org/gems/dry-logic"
|
19
|
+
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-logic.gemspec", "lib/**/*"]
|
20
|
+
spec.bindir = "bin"
|
21
|
+
spec.executables = []
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
25
|
+
spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-logic/blob/main/CHANGELOG.md"
|
26
|
+
spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-logic"
|
27
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-logic/issues"
|
28
|
+
|
29
|
+
spec.required_ruby_version = ">= 2.7.0"
|
30
|
+
|
31
|
+
# to update dependencies edit project.yml
|
32
|
+
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
|
33
|
+
spec.add_runtime_dependency "dry-core", "~> 1.0", "< 2"
|
34
|
+
spec.add_runtime_dependency "zeitwerk", "~> 2.6"
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler"
|
37
|
+
spec.add_development_dependency "rake"
|
38
|
+
spec.add_development_dependency "rspec"
|
23
39
|
end
|
data/lib/dry/logic/appliable.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "singleton"
|
4
|
+
require "delegate"
|
5
|
+
|
6
|
+
module Dry
|
7
|
+
module Logic
|
8
|
+
module Builder
|
9
|
+
IGNORED_OPERATIONS = %i[
|
10
|
+
Abstract
|
11
|
+
Binary
|
12
|
+
Unary
|
13
|
+
].freeze
|
14
|
+
|
15
|
+
IGNORED_PREDICATES = [
|
16
|
+
:predicate
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
# Predicate and operation builder
|
20
|
+
#
|
21
|
+
# @block [Proc]
|
22
|
+
# @return [Builder::Result]
|
23
|
+
# @example Check if input is zero
|
24
|
+
# is_zero = Dry::Logic::Builder.call do
|
25
|
+
# negation { lt?(0) ^ gt?(0) }
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# p is_zero.call(1) # => false
|
29
|
+
# p is_zero.call(0) # => true
|
30
|
+
# p is_zero.call(-1) # => false
|
31
|
+
def call(&context)
|
32
|
+
Context.instance.call(&context)
|
33
|
+
end
|
34
|
+
module_function :call
|
35
|
+
alias_method :build, :call
|
36
|
+
public :call, :build
|
37
|
+
|
38
|
+
class Context
|
39
|
+
include Dry::Logic
|
40
|
+
include Singleton
|
41
|
+
|
42
|
+
module Predicates
|
43
|
+
include Logic::Predicates
|
44
|
+
end
|
45
|
+
|
46
|
+
# @see Builder#call
|
47
|
+
def call(&context)
|
48
|
+
instance_eval(&context)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Defines custom predicate
|
52
|
+
#
|
53
|
+
# @name [Symbol] Name of predicate
|
54
|
+
# @Context [Proc]
|
55
|
+
def predicate(name, &context)
|
56
|
+
if singleton_class.method_defined?(name)
|
57
|
+
singleton_class.undef_method(name)
|
58
|
+
end
|
59
|
+
|
60
|
+
prerdicate = Rule::Predicate.new(context)
|
61
|
+
|
62
|
+
define_singleton_method(name) do |*args|
|
63
|
+
prerdicate.curry(*args)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Defines methods for operations and predicates
|
68
|
+
def initialize
|
69
|
+
Operations.constants(false).each do |name|
|
70
|
+
next if IGNORED_OPERATIONS.include?(name)
|
71
|
+
|
72
|
+
operation = Operations.const_get(name)
|
73
|
+
|
74
|
+
define_singleton_method(name.downcase) do |*args, **kwargs, &block|
|
75
|
+
operation.new(*call(&block), *args, **kwargs)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Predicates::Methods.instance_methods(false).each do |name|
|
80
|
+
unless IGNORED_PREDICATES.include?(name)
|
81
|
+
predicate(name, &Predicates[name])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/dry/logic/evaluator.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'dry/equalizer'
|
3
|
-
require 'dry/logic/operators'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
3
|
module Dry
|
6
4
|
module Logic
|
@@ -24,15 +22,15 @@ module Dry
|
|
24
22
|
end
|
25
23
|
|
26
24
|
def curry(*args)
|
27
|
-
new(rules.map { |rule| rule.curry(*args) }, options)
|
25
|
+
new(rules.map { |rule| rule.curry(*args) }, **options)
|
28
26
|
end
|
29
27
|
|
30
28
|
def new(rules, **new_options)
|
31
|
-
self.class.new(*rules, options
|
29
|
+
self.class.new(*rules, **options, **new_options)
|
32
30
|
end
|
33
31
|
|
34
32
|
def with(new_options)
|
35
|
-
new(rules, options
|
33
|
+
new(rules, **options, **new_options)
|
36
34
|
end
|
37
35
|
|
38
36
|
def to_ast
|
@@ -1,10 +1,16 @@
|
|
1
|
-
|
2
|
-
require 'dry/logic/result'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
module Dry
|
5
4
|
module Logic
|
6
5
|
module Operations
|
7
6
|
class And < Binary
|
7
|
+
attr_reader :hints
|
8
|
+
|
9
|
+
def initialize(*, **)
|
10
|
+
super
|
11
|
+
@hints = options.fetch(:hints, true)
|
12
|
+
end
|
13
|
+
|
8
14
|
def type
|
9
15
|
:and
|
10
16
|
end
|
@@ -22,7 +28,10 @@ module Dry
|
|
22
28
|
Result.new(false, id) { right_result.ast(input) }
|
23
29
|
end
|
24
30
|
else
|
25
|
-
Result.new(false, id)
|
31
|
+
Result.new(false, id) do
|
32
|
+
left_ast = left_result.to_ast
|
33
|
+
hints ? [type, [left_ast, [:hint, right.ast(input)]]] : left_ast
|
34
|
+
end
|
26
35
|
end
|
27
36
|
end
|
28
37
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Dry
|
4
4
|
module Logic
|
@@ -8,9 +8,10 @@ module Dry
|
|
8
8
|
|
9
9
|
attr_reader :right
|
10
10
|
|
11
|
-
def initialize(
|
11
|
+
def initialize(left, right, **options)
|
12
12
|
super
|
13
|
-
@left
|
13
|
+
@left = left
|
14
|
+
@right = right
|
14
15
|
end
|
15
16
|
|
16
17
|
def ast(input = Undefined)
|
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'dry/logic/evaluator'
|
3
|
-
require 'dry/logic/result'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
3
|
module Dry
|
6
4
|
module Logic
|
@@ -10,12 +8,12 @@ module Dry
|
|
10
8
|
|
11
9
|
def self.new(rule, **options)
|
12
10
|
if options[:evaluator]
|
13
|
-
super(rule, options)
|
11
|
+
super(rule, **options)
|
14
12
|
else
|
15
13
|
keys = options.fetch(:keys)
|
16
14
|
evaluator = Evaluator::Set.new(keys)
|
17
15
|
|
18
|
-
super(rule, options
|
16
|
+
super(rule, **options, evaluator: evaluator)
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|