predicate-rb 1.2.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 +7 -0
- data/CHANGELOG.md +64 -0
- data/EXAMPLES.md +4183 -0
- data/LICENSE.txt +21 -0
- data/README.md +764 -0
- data/lib/predicate/combinators.rb +322 -0
- data/lib/predicate/core.rb +403 -0
- data/lib/predicate/dsl.rb +355 -0
- data/lib/predicate/model_integration.rb +410 -0
- data/lib/predicate/railtie.rb +19 -0
- data/lib/predicate/util.rb +36 -0
- data/lib/predicate/validation.rb +244 -0
- data/lib/predicate/version.rb +5 -0
- data/lib/predicate-rb.rb +6 -0
- data/lib/predicate.rb +279 -0
- metadata +85 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Predicate
|
|
4
|
+
# Predicate Combinators
|
|
5
|
+
#
|
|
6
|
+
# Logical operators and combinators for creating complex predicate logic.
|
|
7
|
+
#
|
|
8
|
+
# Features::
|
|
9
|
+
# * AND/OR/NOT helpers
|
|
10
|
+
# * Chainable predicate combinations
|
|
11
|
+
# * Short-circuit evaluation
|
|
12
|
+
# * Nested predicate composition
|
|
13
|
+
# * English-like helpers (`all_of`, `any_of`, `none_of`)
|
|
14
|
+
# * Conditional predicates (`when_present`, `when_blank`)
|
|
15
|
+
#
|
|
16
|
+
# Usage::
|
|
17
|
+
# Predicate.define(:assessment) do
|
|
18
|
+
# has_media { |s| is_present?(s[:media_attachment_ids]) }
|
|
19
|
+
# has_title { |s| is_present?(s[:title]) }
|
|
20
|
+
# has_description { |s| is_present?(s[:description]) }
|
|
21
|
+
#
|
|
22
|
+
# content_ready { |s| all_of(:has_title, :has_description).call(s) }
|
|
23
|
+
# any_content { |s| any_of(:has_title, :has_description).call(s) }
|
|
24
|
+
# not_empty { |s| not_predicate(:has_media).call(s) }
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# @author Nauman Tariq
|
|
28
|
+
# @version 1.0.0
|
|
29
|
+
module Combinators
|
|
30
|
+
# COMBINATOR BASE
|
|
31
|
+
# Base class for all combinators providing common functionality
|
|
32
|
+
#
|
|
33
|
+
# Features:
|
|
34
|
+
# * Predicate resolution and execution
|
|
35
|
+
# * Error handling and graceful fallbacks
|
|
36
|
+
# * Performance tracking and caching
|
|
37
|
+
# * Core integration for helper methods
|
|
38
|
+
class Base
|
|
39
|
+
attr_reader :core
|
|
40
|
+
|
|
41
|
+
def initialize(core)
|
|
42
|
+
@core = core
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Call the combinator with a state
|
|
46
|
+
# @param state [Hash] The state to evaluate
|
|
47
|
+
# @return [Boolean] The result of the combination
|
|
48
|
+
def call(state)
|
|
49
|
+
raise NotImplementedError, 'Combinator must implement #call'
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
# Resolve a predicate reference to an actual callable
|
|
55
|
+
# @param predicate [Symbol, String, Proc, Combinator] The predicate reference
|
|
56
|
+
# @return [Proc] The callable predicate
|
|
57
|
+
def resolve_predicate(predicate)
|
|
58
|
+
case predicate
|
|
59
|
+
when Symbol, String, Proc, Base
|
|
60
|
+
# Store the symbol/string for lazy resolution or handle combinator objects
|
|
61
|
+
predicate
|
|
62
|
+
else
|
|
63
|
+
raise ArgumentError, "Invalid predicate type: #{predicate.class}"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Resolve and execute a predicate (lazy resolution)
|
|
68
|
+
# @param predicate [Symbol, String, Proc, Combinator] The predicate to execute
|
|
69
|
+
# @param state [Hash] The state to pass to the predicate
|
|
70
|
+
# @return [Boolean] The result of the predicate
|
|
71
|
+
def resolve_and_execute(predicate, state)
|
|
72
|
+
case predicate
|
|
73
|
+
when Symbol, String
|
|
74
|
+
@core.call(predicate.to_sym, state)
|
|
75
|
+
when Proc, Base
|
|
76
|
+
# Handle combinator objects and procs
|
|
77
|
+
predicate.call(state)
|
|
78
|
+
else
|
|
79
|
+
raise ArgumentError, "Invalid predicate type: #{predicate.class}"
|
|
80
|
+
end
|
|
81
|
+
rescue StandardError => e
|
|
82
|
+
Rails.logger.error "Combinator predicate failed: #{e.message}" if defined?(Rails) && Rails.respond_to?(:logger)
|
|
83
|
+
false
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Execute a predicate safely with error handling
|
|
87
|
+
# @param predicate [Proc] The predicate to execute
|
|
88
|
+
# @param state [Hash] The state to pass to the predicate
|
|
89
|
+
# @return [Boolean] The result of the predicate
|
|
90
|
+
def execute_predicate(predicate, state)
|
|
91
|
+
predicate.call(state)
|
|
92
|
+
rescue StandardError => e
|
|
93
|
+
Rails.logger.error "Combinator predicate failed: #{e.message}" if defined?(Rails) && Rails.respond_to?(:logger)
|
|
94
|
+
false
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# ALL_OF (AND)
|
|
99
|
+
# Logical AND combinator - all predicates must return true
|
|
100
|
+
#
|
|
101
|
+
# Features:
|
|
102
|
+
# * Short-circuit evaluation (stops on first false)
|
|
103
|
+
# * Performance optimization for large predicate lists
|
|
104
|
+
# * Error handling with graceful fallbacks
|
|
105
|
+
#
|
|
106
|
+
# Usage:
|
|
107
|
+
# all_of(:has_title, :has_description, :has_media).call(state)
|
|
108
|
+
class AllOf < Base
|
|
109
|
+
def initialize(core, *predicates)
|
|
110
|
+
super(core)
|
|
111
|
+
@predicates = predicates.map { |p| resolve_predicate(p) }
|
|
112
|
+
raise ArgumentError, 'At least one predicate is required' if @predicates.empty?
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Execute all predicates with AND logic (short-circuit on false)
|
|
116
|
+
# @param state [Hash] The state to evaluate
|
|
117
|
+
# @return [Boolean] True if all predicates return true
|
|
118
|
+
def call(state)
|
|
119
|
+
@predicates.all? { |predicate| resolve_and_execute(predicate, state) }
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# ANY_OF (OR)
|
|
124
|
+
# Logical OR combinator - at least one predicate must return true
|
|
125
|
+
#
|
|
126
|
+
# Features:
|
|
127
|
+
# * Short-circuit evaluation (stops on first true)
|
|
128
|
+
# * Performance optimization for large predicate lists
|
|
129
|
+
# * Error handling with graceful fallbacks
|
|
130
|
+
#
|
|
131
|
+
# Usage:
|
|
132
|
+
# any_of(:has_title, :has_description, :has_media).call(state)
|
|
133
|
+
class AnyOf < Base
|
|
134
|
+
def initialize(core, *predicates)
|
|
135
|
+
super(core)
|
|
136
|
+
@predicates = predicates.map { |p| resolve_predicate(p) }
|
|
137
|
+
raise ArgumentError, 'At least one predicate is required' if @predicates.empty?
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Execute all predicates with OR logic (short-circuit on true)
|
|
141
|
+
# @param state [Hash] The state to evaluate
|
|
142
|
+
# @return [Boolean] True if any predicate returns true
|
|
143
|
+
def call(state)
|
|
144
|
+
@predicates.any? { |predicate| resolve_and_execute(predicate, state) }
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# NONE_OF (NOR)
|
|
149
|
+
# Logical NOR combinator - none of the predicates should return true
|
|
150
|
+
#
|
|
151
|
+
# Features:
|
|
152
|
+
# * Short-circuit evaluation (stops on first true)
|
|
153
|
+
# * Performance optimization for large predicate lists
|
|
154
|
+
# * Error handling with graceful fallbacks
|
|
155
|
+
#
|
|
156
|
+
# Usage:
|
|
157
|
+
# none_of(:has_errors, :is_invalid, :is_expired).call(state)
|
|
158
|
+
class NoneOf < Base
|
|
159
|
+
def initialize(core, *predicates)
|
|
160
|
+
super(core)
|
|
161
|
+
@predicates = predicates.map { |p| resolve_predicate(p) }
|
|
162
|
+
raise ArgumentError, 'At least one predicate is required' if @predicates.empty?
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Execute all predicates with NOR logic (none should be true)
|
|
166
|
+
# @param state [Hash] The state to evaluate
|
|
167
|
+
# @return [Boolean] True if none of the predicates return true
|
|
168
|
+
def call(state)
|
|
169
|
+
@predicates.none? { |predicate| resolve_and_execute(predicate, state) }
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# NOT
|
|
174
|
+
# Logical NOT combinator - negates a single predicate
|
|
175
|
+
#
|
|
176
|
+
# Features:
|
|
177
|
+
# * Simple negation of any predicate
|
|
178
|
+
# * Error handling with graceful fallbacks
|
|
179
|
+
# * Support for symbol, string, and proc predicates
|
|
180
|
+
#
|
|
181
|
+
# Usage:
|
|
182
|
+
# not(:has_errors).call(state)
|
|
183
|
+
class Not < Base
|
|
184
|
+
def initialize(core, predicate)
|
|
185
|
+
super(core)
|
|
186
|
+
@predicate = resolve_predicate(predicate)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Execute predicate and negate the result
|
|
190
|
+
# @param state [Hash] The state to evaluate
|
|
191
|
+
# @return [Boolean] The negated result of the predicate
|
|
192
|
+
def call(state)
|
|
193
|
+
!resolve_and_execute(@predicate, state)
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# WHEN_PRESENT
|
|
198
|
+
# Conditional combinator - only evaluates predicate when field is present
|
|
199
|
+
#
|
|
200
|
+
# Features:
|
|
201
|
+
# * Conditional evaluation based on field presence
|
|
202
|
+
# * Prevents errors when evaluating empty/nil fields
|
|
203
|
+
# * Customizable default return value
|
|
204
|
+
#
|
|
205
|
+
# Usage:
|
|
206
|
+
# when_present(:title) { |s| min_length?(s[:title], 3) }.call(state)
|
|
207
|
+
class WhenPresent < Base
|
|
208
|
+
def initialize(core, field, default_value: true, &predicate)
|
|
209
|
+
super(core)
|
|
210
|
+
@field = field.to_sym
|
|
211
|
+
@predicate = predicate
|
|
212
|
+
@default_value = default_value
|
|
213
|
+
raise ArgumentError, 'Predicate block is required' unless @predicate
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Execute predicate only when field is present
|
|
217
|
+
# @param state [Hash] The state to evaluate
|
|
218
|
+
# @return [Boolean] The result of the predicate or default value
|
|
219
|
+
def call(state)
|
|
220
|
+
return @default_value unless @core.is_present?(state[@field])
|
|
221
|
+
|
|
222
|
+
resolve_and_execute(@predicate, state)
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# WHEN_BLANK
|
|
227
|
+
# Conditional combinator - only evaluates predicate when field is blank
|
|
228
|
+
#
|
|
229
|
+
# Features:
|
|
230
|
+
# * Conditional evaluation based on field absence
|
|
231
|
+
# * Useful for default value logic
|
|
232
|
+
# * Customizable default return value
|
|
233
|
+
#
|
|
234
|
+
# Usage:
|
|
235
|
+
# when_blank(:title) { |s| has_default_title?(s) }.call(state)
|
|
236
|
+
class WhenBlank < Base
|
|
237
|
+
def initialize(core, field, default_value: false, &predicate)
|
|
238
|
+
super(core)
|
|
239
|
+
@field = field.to_sym
|
|
240
|
+
@predicate = predicate
|
|
241
|
+
@default_value = default_value
|
|
242
|
+
raise ArgumentError, 'Predicate block is required' unless @predicate
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Execute predicate only when field is blank
|
|
246
|
+
# @param state [Hash] The state to evaluate
|
|
247
|
+
# @return [Boolean] The result of the predicate or default value
|
|
248
|
+
def call(state)
|
|
249
|
+
return @default_value unless @core.is_blank?(state[@field])
|
|
250
|
+
|
|
251
|
+
resolve_and_execute(@predicate, state)
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# COMBINATOR HELPERS
|
|
256
|
+
# Helper methods for creating combinators with English-like syntax
|
|
257
|
+
#
|
|
258
|
+
# These are mixed into the DSL Builder to provide a natural syntax
|
|
259
|
+
module Helpers
|
|
260
|
+
# Create an ALL_OF combinator (logical AND)
|
|
261
|
+
# @param predicates [Array<Symbol, String, Proc>] Predicates to combine
|
|
262
|
+
# @return [AllOf] The combinator instance
|
|
263
|
+
# @example
|
|
264
|
+
# all_of(:has_title, :has_description)
|
|
265
|
+
def all_of(*predicates)
|
|
266
|
+
AllOf.new(@core, *predicates)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Create an ANY_OF combinator (logical OR)
|
|
270
|
+
# @param predicates [Array<Symbol, String, Proc>] Predicates to combine
|
|
271
|
+
# @return [AnyOf] The combinator instance
|
|
272
|
+
# @example
|
|
273
|
+
# any_of(:has_title, :has_description)
|
|
274
|
+
def any_of(*predicates)
|
|
275
|
+
AnyOf.new(@core, *predicates)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Create a NONE_OF combinator (logical NOR)
|
|
279
|
+
# @param predicates [Array<Symbol, String, Proc>] Predicates to combine
|
|
280
|
+
# @return [NoneOf] The combinator instance
|
|
281
|
+
# @example
|
|
282
|
+
# none_of(:has_errors, :is_invalid)
|
|
283
|
+
def none_of(*predicates)
|
|
284
|
+
NoneOf.new(@core, *predicates)
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Create a NOT combinator (logical NOT)
|
|
288
|
+
# @param predicate [Symbol, String, Proc] Predicate to negate
|
|
289
|
+
# @return [Not] The combinator instance
|
|
290
|
+
# @example
|
|
291
|
+
# not_predicate(:has_errors)
|
|
292
|
+
def not_predicate(predicate)
|
|
293
|
+
Not.new(@core, predicate)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
# Alias for backwards compatibility, but avoid 'not' keyword
|
|
297
|
+
alias negate not_predicate
|
|
298
|
+
|
|
299
|
+
# Create a WHEN_PRESENT combinator
|
|
300
|
+
# @param field [Symbol, String] Field to check for presence
|
|
301
|
+
# @param default_value [Boolean] Default value when field is not present
|
|
302
|
+
# @param block [Proc] Predicate to execute when field is present
|
|
303
|
+
# @return [WhenPresent] The combinator instance
|
|
304
|
+
# @example
|
|
305
|
+
# when_present(:title) { |s| min_length?(s[:title], 3) }
|
|
306
|
+
def when_present(field, default_value: true, &block)
|
|
307
|
+
WhenPresent.new(@core, field, default_value: default_value, &block)
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Create a WHEN_BLANK combinator
|
|
311
|
+
# @param field [Symbol, String] Field to check for blankness
|
|
312
|
+
# @param default_value [Boolean] Default value when field is not blank
|
|
313
|
+
# @param block [Proc] Predicate to execute when field is blank
|
|
314
|
+
# @return [WhenBlank] The combinator instance
|
|
315
|
+
# @example
|
|
316
|
+
# when_blank(:title) { |s| has_default_title?(s) }
|
|
317
|
+
def when_blank(field, default_value: false, &block)
|
|
318
|
+
WhenBlank.new(@core, field, default_value: default_value, &block)
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
end
|