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.
@@ -0,0 +1,355 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Predicate
4
+ # Predicate DSL
5
+ #
6
+ # Meta-programming DSL that provides English-like syntax and automatic method
7
+ # generation for predicate definitions.
8
+ #
9
+ # Features::
10
+ # * `method_missing` for predicate definitions
11
+ # * Helper-method delegation (e.g., `is_present?`, `is_blank?`)
12
+ # * Section-based predicate grouping
13
+ # * Auto-generation of predicate methods
14
+ # * Rails validation detection and helper generation
15
+ # * Chainable predicate definitions
16
+ #
17
+ # Usage::
18
+ # Predicate.define(:assessment) do
19
+ # has_media { |s| is_present?(s[:media_attachment_ids]) }
20
+ # has_title { |s| is_present?(s[:title]) }
21
+ #
22
+ # section :validation do
23
+ # required_fields { |s| is_present?(s[:name]) && is_present?(s[:email]) }
24
+ # valid_format { |s| matches?(s[:email], EMAIL_REGEXP) }
25
+ # end
26
+ #
27
+ # wizard_complete { |s| has_media(s) && has_title(s) }
28
+ # end
29
+ #
30
+ # @author Nauman Tariq
31
+ # @version 1.0.0
32
+ module DSL
33
+ # DSL BUILDER
34
+ # The heart of the DSL - uses method_missing to create an English-like
35
+ # interface for defining predicates with automatic method generation
36
+ #
37
+ # Features:
38
+ # * Direct method definition (no explicit name() calls)
39
+ # * Automatic predicate registration with Core engine
40
+ # * Section-based grouping support
41
+ # * English helper methods delegation
42
+ # * Chainable predicate definitions
43
+ # * Performance optimization via caching
44
+ #
45
+ # Usage:
46
+ # builder = DSL::Builder.new(:assessment, core_instance)
47
+ # builder.instance_eval(&block)
48
+ # builder.build
49
+ class Builder
50
+ attr_reader :entity, :core, :current_section
51
+
52
+ def initialize(entity, core = nil, cache_ttl: nil, thread_safe: false)
53
+ @entity = entity.to_sym
54
+ @cache_ttl = cache_ttl
55
+ @thread_safe = thread_safe
56
+ # Create core immediately with parameters (needed for predicate definitions)
57
+ @core = core || Core.new(@entity, cache_ttl: cache_ttl, thread_safe: thread_safe)
58
+ @current_section = nil
59
+ @defined_predicates = {}
60
+ end
61
+
62
+ # Build and return the final core instance
63
+ # @return [Predicate::Core] The configured core instance
64
+ def build(_cache_ttl: nil, _thread_safe: false)
65
+ # Core is already created in initialize with correct parameters
66
+ @core
67
+ end
68
+
69
+ # Define a section for grouping related predicates
70
+ # @param section_name [Symbol, String] The section name
71
+ # @param block [Proc] Block containing section predicates
72
+ # @example
73
+ # section :validation do
74
+ # required_fields { |s| is_present?(s[:name]) && is_present?(s[:email]) }
75
+ # valid_format { |s| matches?(s[:email], EMAIL_REGEXP) }
76
+ # end
77
+ def section(section_name, &block)
78
+ raise ArgumentError, 'Section block is required' unless block_given?
79
+
80
+ @current_section = section_name.to_sym
81
+
82
+ # Create section builder for scoped predicate definitions
83
+ section_builder = SectionBuilder.new(@entity, @core, section_name)
84
+ section_builder.instance_eval(&block)
85
+
86
+ @current_section = nil
87
+ self
88
+ end
89
+
90
+ # Enable calling existing predicates within new predicate definitions
91
+ # @param predicate_name [Symbol] The predicate name to call
92
+ # @param state [Hash] The state to evaluate
93
+ # @return [Boolean] The predicate result
94
+ def call_predicate(predicate_name, state)
95
+ @core.call(predicate_name, state)
96
+ end
97
+
98
+ # Delegate all helper methods to the core instance
99
+ # This enables using helper methods directly in predicate definitions
100
+ def method_missing(method_name, *args, &block)
101
+ # If block is given, this is a predicate definition
102
+ if block_given?
103
+ define_predicate(method_name, &block)
104
+ return self
105
+ end
106
+
107
+ # If it's a helper method, delegate to core
108
+ return @core.public_send(method_name, *args) if @core.respond_to?(method_name)
109
+
110
+ # If it's a defined predicate being called, execute it
111
+ return call_predicate(method_name, args.first) if args.length == 1 && args.first.is_a?(Hash)
112
+
113
+ # Otherwise, raise NoMethodError
114
+ super
115
+ end
116
+
117
+ def respond_to_missing?(method_name, include_private = false)
118
+ @core.respond_to?(method_name, include_private) ||
119
+ @defined_predicates.key?(method_name.to_sym) ||
120
+ super
121
+ end
122
+
123
+ private
124
+
125
+ # Define a predicate and register it with the core
126
+ # @param predicate_name [Symbol] The predicate name
127
+ # @param block [Proc] The predicate logic
128
+ def define_predicate(predicate_name, &block)
129
+ predicate_name = predicate_name.to_sym
130
+
131
+ # Store reference for respond_to_missing?
132
+ @defined_predicates[predicate_name] = true
133
+
134
+ if @current_section
135
+ # Add to current section
136
+ @core.add_section(@current_section) do |predicates|
137
+ predicates[predicate_name] = block
138
+ end
139
+ else
140
+ # Add as direct predicate
141
+ @core.add_predicate(predicate_name, &block)
142
+ end
143
+ end
144
+ end
145
+
146
+ # SECTION BUILDER
147
+ # Specialized builder for section-scoped predicate definitions
148
+ #
149
+ # Features:
150
+ # * Scoped predicate definitions within sections
151
+ # * Automatic section registration
152
+ # * Helper method delegation
153
+ # * Cross-section predicate calling
154
+ #
155
+ # Usage:
156
+ # section_builder = SectionBuilder.new(:assessment, core, :validation)
157
+ # section_builder.instance_eval(&block)
158
+ class SectionBuilder
159
+ attr_reader :entity, :core, :section_name
160
+
161
+ def initialize(entity, core, section_name)
162
+ @entity = entity.to_sym
163
+ @core = core
164
+ @section_name = section_name.to_sym
165
+ @section_predicates = {}
166
+ end
167
+
168
+ # Build the section and register it with the core
169
+ def build
170
+ @core.add_section(@section_name) do |predicates|
171
+ @section_predicates.each do |name, block|
172
+ predicates[name] = block
173
+ end
174
+ end
175
+
176
+ @core
177
+ end
178
+
179
+ # Enable calling predicates from other sections or direct predicates
180
+ # @param predicate_name [Symbol] The predicate name to call
181
+ # @param state [Hash] The state to evaluate
182
+ # @return [Boolean] The predicate result
183
+ def call_predicate(predicate_name, state)
184
+ @core.call(predicate_name, state)
185
+ end
186
+
187
+ # Delegate all helper methods to the core instance
188
+ def method_missing(method_name, *args, &block)
189
+ # If block is given, this is a predicate definition
190
+ if block_given?
191
+ @section_predicates[method_name.to_sym] = block
192
+ build # Rebuild section with new predicate
193
+ return self
194
+ end
195
+
196
+ # If it's a helper method, delegate to core
197
+ return @core.public_send(method_name, *args) if @core.respond_to?(method_name)
198
+
199
+ # If it's a predicate being called, execute it
200
+ return call_predicate(method_name, args.first) if args.length == 1 && args.first.is_a?(Hash)
201
+
202
+ # Otherwise, raise NoMethodError
203
+ super
204
+ end
205
+
206
+ def respond_to_missing?(method_name, include_private = false)
207
+ @core.respond_to?(method_name, include_private) ||
208
+ @section_predicates.key?(method_name.to_sym) ||
209
+ super
210
+ end
211
+
212
+ private
213
+
214
+ # Implement define_predicate for ValidationHelpers compatibility
215
+ # @param name [Symbol] The predicate name
216
+ # @param block [Proc] The predicate logic
217
+ def define_predicate(name, &block)
218
+ @section_predicates[name.to_sym] = block
219
+ build # Rebuild section with new predicate
220
+ self
221
+ end
222
+ end
223
+
224
+ # VALIDATION HELPERS
225
+ # Rails-style validation helpers for common predicate patterns
226
+ #
227
+ # Features:
228
+ # * Rails validation pattern detection
229
+ # * Automatic predicate generation from validations
230
+ # * Common validation patterns (presence, format, numericality)
231
+ # * Chainable validation definitions
232
+ #
233
+ # Usage:
234
+ # builder.validates_presence_of(:name, :email)
235
+ # builder.validates_format_of(:email, with: EMAIL_REGEXP)
236
+ # builder.validates_numericality_of(:age, greater_than: 0)
237
+ module ValidationHelpers
238
+ # Generate presence validation predicates
239
+ # @param fields [Array<Symbol>] Fields to validate for presence
240
+ # @example
241
+ # validates_presence_of(:name, :email)
242
+ # # Generates: has_name?, has_email?, required_fields?
243
+ def validates_presence_of(*fields)
244
+ fields.each do |field|
245
+ predicate_name = :"has_#{field}"
246
+ define_predicate(predicate_name) do |state|
247
+ @core.is_present?(state[field])
248
+ end
249
+ end
250
+
251
+ # Generate combined predicate
252
+ if fields.length > 1
253
+ define_predicate(:required_fields) do |state|
254
+ fields.all? { |field| @core.is_present?(state[field]) }
255
+ end
256
+ end
257
+
258
+ self
259
+ end
260
+
261
+ # Generate format validation predicates
262
+ # @param field [Symbol] Field to validate
263
+ # @param options [Hash] Validation options
264
+ # @option options [Regexp] :with Pattern to match against
265
+ # @example
266
+ # validates_format_of(:email, with: EMAIL_REGEXP)
267
+ # # Generates: valid_email_format?
268
+ def validates_format_of(field, options = {})
269
+ pattern = options[:with]
270
+ raise ArgumentError, 'Pattern (:with) is required' unless pattern
271
+
272
+ predicate_name = :"valid_#{field}_format"
273
+ define_predicate(predicate_name) do |state|
274
+ @core.matches?(state[field], pattern)
275
+ end
276
+
277
+ self
278
+ end
279
+
280
+ # Generate numericality validation predicates
281
+ # @param field [Symbol] Field to validate
282
+ # @param options [Hash] Validation options
283
+ # @option options [Numeric] :greater_than Minimum value (exclusive)
284
+ # @option options [Numeric] :greater_than_or_equal_to Minimum value (inclusive)
285
+ # @option options [Numeric] :less_than Maximum value (exclusive)
286
+ # @option options [Numeric] :less_than_or_equal_to Maximum value (inclusive)
287
+ # @example
288
+ # validates_numericality_of(:age, greater_than: 0, less_than: 150)
289
+ # # Generates: valid_age_range?
290
+ def validates_numericality_of(field, options = {})
291
+ predicate_name = :"valid_#{field}"
292
+
293
+ define_predicate(predicate_name) do |state|
294
+ value = state[field]
295
+ return false unless value.is_a?(Numeric)
296
+
297
+ checks = []
298
+ checks << @core.greater_than?(value, options[:greater_than]) if options[:greater_than]
299
+ if options[:greater_than_or_equal_to]
300
+ checks << @core.greater_than_or_equal_to?(value,
301
+ options[:greater_than_or_equal_to])
302
+ end
303
+ checks << @core.less_than?(value, options[:less_than]) if options[:less_than]
304
+ if options[:less_than_or_equal_to]
305
+ checks << @core.less_than_or_equal_to?(value,
306
+ options[:less_than_or_equal_to])
307
+ end
308
+
309
+ checks.all?
310
+ end
311
+
312
+ self
313
+ end
314
+
315
+ # Generate length validation predicates
316
+ # @param field [Symbol] Field to validate
317
+ # @param options [Hash] Validation options
318
+ # @option options [Integer] :minimum Minimum length
319
+ # @option options [Integer] :maximum Maximum length
320
+ # @option options [Range] :in Length range
321
+ # @example
322
+ # validates_length_of(:password, minimum: 8, maximum: 128)
323
+ # # Generates: valid_password_length?
324
+ def validates_length_of(field, options = {})
325
+ predicate_name = :"valid_#{field}_length"
326
+
327
+ define_predicate(predicate_name) do |state|
328
+ value = state[field]
329
+ return false unless value.respond_to?(:length)
330
+
331
+ length = value.length
332
+ checks = []
333
+ checks << @core.min_length?(value, options[:minimum]) if options[:minimum]
334
+ checks << @core.max_length?(value, options[:maximum]) if options[:maximum]
335
+ checks << @core.in_range?(length, options[:in]) if options[:in]
336
+
337
+ checks.all?
338
+ end
339
+
340
+ self
341
+ end
342
+
343
+ private
344
+
345
+ # Define a predicate (to be implemented by including class)
346
+ def define_predicate(name, &block)
347
+ raise NotImplementedError, 'define_predicate must be implemented by including class'
348
+ end
349
+ end
350
+
351
+ # Include validation helpers in Builder
352
+ Builder.include(ValidationHelpers)
353
+ SectionBuilder.include(ValidationHelpers)
354
+ end
355
+ end