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,244 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Predicate
|
|
4
|
+
# Validation Helpers
|
|
5
|
+
#
|
|
6
|
+
# A collection of pure functions for validating values.
|
|
7
|
+
# These are mixed into Predicate::Core and available in the DSL.
|
|
8
|
+
#
|
|
9
|
+
# Note: Helper names use the +is_+ prefix (e.g. +is_present?+, +is_blank?+)
|
|
10
|
+
# to avoid conflicts with ActiveSupport's +Object#present?+ and +Object#blank?+
|
|
11
|
+
# which are zero-argument methods defined on every object in Rails.
|
|
12
|
+
module Validation
|
|
13
|
+
# Check if a value is present (not nil, not empty, not whitespace-only).
|
|
14
|
+
#
|
|
15
|
+
# Named +is_present?+ to avoid collision with ActiveSupport's +Object#present?+
|
|
16
|
+
# which takes no arguments and checks +self+.
|
|
17
|
+
#
|
|
18
|
+
# @param value [Object] The value to check
|
|
19
|
+
# @return [Boolean] True if value is present (not nil or empty)
|
|
20
|
+
# @example
|
|
21
|
+
# is_present?("hello") # => true
|
|
22
|
+
# is_present?(nil) # => false
|
|
23
|
+
# is_present?("") # => false
|
|
24
|
+
# is_present?(" ") # => false
|
|
25
|
+
def is_present?(value)
|
|
26
|
+
return false if value.nil?
|
|
27
|
+
return false if value.respond_to?(:empty?) && value.empty?
|
|
28
|
+
# Handle whitespace-only strings
|
|
29
|
+
return false if value.is_a?(String) && value.strip.empty?
|
|
30
|
+
|
|
31
|
+
true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Check if a value is blank (nil, empty, or whitespace-only).
|
|
35
|
+
#
|
|
36
|
+
# Named +is_blank?+ to avoid collision with ActiveSupport's +Object#blank?+
|
|
37
|
+
# which takes no arguments and checks +self+.
|
|
38
|
+
#
|
|
39
|
+
# @param value [Object] The value to check
|
|
40
|
+
# @return [Boolean] True if value is blank (nil or empty)
|
|
41
|
+
# @example
|
|
42
|
+
# is_blank?(nil) # => true
|
|
43
|
+
# is_blank?("") # => true
|
|
44
|
+
# is_blank?(" ") # => true
|
|
45
|
+
# is_blank?("hello") # => false
|
|
46
|
+
def is_blank?(value)
|
|
47
|
+
return true if value.nil?
|
|
48
|
+
return true if value.respond_to?(:empty?) && value.empty?
|
|
49
|
+
# Handle whitespace-only strings
|
|
50
|
+
return true if value.is_a?(String) && value.strip.empty?
|
|
51
|
+
|
|
52
|
+
false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @param value [Object] The value to check
|
|
56
|
+
# @return [Boolean] True if value is falsy
|
|
57
|
+
def not?(value)
|
|
58
|
+
!value
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @param value [Object] The value to check
|
|
62
|
+
# @return [Boolean] True if value is truthy
|
|
63
|
+
def truthy?(value)
|
|
64
|
+
!!value
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @param value [Object] The value to check
|
|
68
|
+
# @return [Boolean] True if value is falsy
|
|
69
|
+
def falsy?(value)
|
|
70
|
+
!value
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @param value [Object] The value to check
|
|
74
|
+
# @return [Boolean] True if value is a positive number
|
|
75
|
+
def positive?(value)
|
|
76
|
+
value.is_a?(Numeric) && value.positive?
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# @param value [Object] The value to check
|
|
80
|
+
# @return [Boolean] True if value is a negative number
|
|
81
|
+
def negative?(value)
|
|
82
|
+
value.is_a?(Numeric) && value.negative?
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @param value [Object] The value to check
|
|
86
|
+
# @return [Boolean] True if value is zero
|
|
87
|
+
def zero?(value)
|
|
88
|
+
value.is_a?(Numeric) && value.zero?
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# @param value [Object] The value to check
|
|
92
|
+
# @return [Boolean] True if value is an array with elements
|
|
93
|
+
def has_elements?(value)
|
|
94
|
+
value.is_a?(Array) && !value.empty?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# @param value [Object] The value to check
|
|
98
|
+
# @return [Boolean] True if value is a hash with keys
|
|
99
|
+
def has_keys?(value)
|
|
100
|
+
value.is_a?(Hash) && !value.empty?
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# @param value [Object] The value to check
|
|
104
|
+
# @param min_length [Integer] Minimum length
|
|
105
|
+
# @return [Boolean] True if value has minimum length
|
|
106
|
+
def min_length?(value, min_length)
|
|
107
|
+
return false unless value.respond_to?(:length)
|
|
108
|
+
|
|
109
|
+
value.length >= min_length
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# @param value [Object] The value to check
|
|
113
|
+
# @param max_length [Integer] Maximum length
|
|
114
|
+
# @return [Boolean] True if value has maximum length
|
|
115
|
+
def max_length?(value, max_length)
|
|
116
|
+
return false unless value.respond_to?(:length)
|
|
117
|
+
|
|
118
|
+
value.length <= max_length
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# @param value [Object] The value to check
|
|
122
|
+
# @param min [Numeric] Minimum value
|
|
123
|
+
# @return [Boolean] True if value is greater than or equal to min
|
|
124
|
+
def min_value?(value, min)
|
|
125
|
+
return false unless value.is_a?(Numeric)
|
|
126
|
+
|
|
127
|
+
value >= min
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# @param value [Object] The value to check
|
|
131
|
+
# @param max [Numeric] Maximum value
|
|
132
|
+
# @return [Boolean] True if value is less than or equal to max
|
|
133
|
+
def max_value?(value, max)
|
|
134
|
+
return false unless value.is_a?(Numeric)
|
|
135
|
+
|
|
136
|
+
value <= max
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# @param value [Object] The value to check
|
|
140
|
+
# @param range [Range] The range to check against
|
|
141
|
+
# @return [Boolean] True if value is within range
|
|
142
|
+
def in_range?(value, range)
|
|
143
|
+
return false unless value.is_a?(Numeric)
|
|
144
|
+
|
|
145
|
+
range.include?(value)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# @param value [Object] The value to check
|
|
149
|
+
# @param pattern [Regexp] The pattern to match
|
|
150
|
+
# @return [Boolean] True if value matches pattern
|
|
151
|
+
def matches?(value, pattern)
|
|
152
|
+
return false unless value.respond_to?(:match)
|
|
153
|
+
|
|
154
|
+
!value.match(pattern).nil?
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# @param value [Object] The value to check
|
|
158
|
+
# @param type [Class] The type to check against
|
|
159
|
+
# @return [Boolean] True if value is of the specified type
|
|
160
|
+
def is_type?(value, type)
|
|
161
|
+
value.is_a?(type)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# @param value [Object] The value to check
|
|
165
|
+
# @param types [Array<Class>] The types to check against
|
|
166
|
+
# @return [Boolean] True if value is one of the specified types
|
|
167
|
+
def is_one_of?(value, types)
|
|
168
|
+
types.any? { |type| value.is_a?(type) }
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# @param value [Object] The value to check
|
|
172
|
+
# @param values [Array] The values to check against
|
|
173
|
+
# @return [Boolean] True if value is one of the specified values
|
|
174
|
+
def is_one_of_values?(value, values)
|
|
175
|
+
values.include?(value)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# @param value [Object] The value to check
|
|
179
|
+
# @param values [Array, Enumerable] The values to check against
|
|
180
|
+
# @return [Boolean] True if value is not one of the specified values
|
|
181
|
+
def is_not_one_of_values?(value, values)
|
|
182
|
+
# Guard against nil
|
|
183
|
+
return true if values.nil?
|
|
184
|
+
|
|
185
|
+
# Guard against non-collection types (String has include? but for substring search)
|
|
186
|
+
return true unless values.is_a?(Enumerable) || values.is_a?(Range)
|
|
187
|
+
|
|
188
|
+
!values.include?(value)
|
|
189
|
+
rescue StandardError
|
|
190
|
+
# If anything goes wrong, treat as not included
|
|
191
|
+
true
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# @param value [Object] The value to check
|
|
195
|
+
# @param other [Object] The other value to compare
|
|
196
|
+
# @return [Boolean] True if values are equal
|
|
197
|
+
def equal_to?(value, other)
|
|
198
|
+
value == other
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# @param value [Object] The value to check
|
|
202
|
+
# @param other [Object] The other value to compare
|
|
203
|
+
# @return [Boolean] True if values are not equal
|
|
204
|
+
def not_equal_to?(value, other)
|
|
205
|
+
value != other
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# @param value [Object] The value to check
|
|
209
|
+
# @param other [Object] The other value to compare
|
|
210
|
+
# @return [Boolean] True if value is greater than other
|
|
211
|
+
def greater_than?(value, other)
|
|
212
|
+
return false unless value.is_a?(Numeric) && other.is_a?(Numeric)
|
|
213
|
+
|
|
214
|
+
value > other
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# @param value [Object] The value to check
|
|
218
|
+
# @param other [Object] The other value to compare
|
|
219
|
+
# @return [Boolean] True if value is less than other
|
|
220
|
+
def less_than?(value, other)
|
|
221
|
+
return false unless value.is_a?(Numeric) && other.is_a?(Numeric)
|
|
222
|
+
|
|
223
|
+
value < other
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# @param value [Object] The value to check
|
|
227
|
+
# @param other [Object] The other value to compare
|
|
228
|
+
# @return [Boolean] True if value is greater than or equal to other
|
|
229
|
+
def greater_than_or_equal_to?(value, other)
|
|
230
|
+
return false unless value.is_a?(Numeric) && other.is_a?(Numeric)
|
|
231
|
+
|
|
232
|
+
value >= other
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# @param value [Object] The value to check
|
|
236
|
+
# @param other [Object] The other value to compare
|
|
237
|
+
# @return [Boolean] True if value is less than or equal to other
|
|
238
|
+
def less_than_or_equal_to?(value, other)
|
|
239
|
+
return false unless value.is_a?(Numeric) && other.is_a?(Numeric)
|
|
240
|
+
|
|
241
|
+
value <= other
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
end
|
data/lib/predicate-rb.rb
ADDED
data/lib/predicate.rb
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'predicate/version'
|
|
4
|
+
require_relative 'predicate/validation'
|
|
5
|
+
require_relative 'predicate/util'
|
|
6
|
+
require_relative 'predicate/core'
|
|
7
|
+
require_relative 'predicate/dsl'
|
|
8
|
+
require_relative 'predicate/combinators'
|
|
9
|
+
require_relative 'predicate/model_integration'
|
|
10
|
+
|
|
11
|
+
# Predicate Library
|
|
12
|
+
#
|
|
13
|
+
# A super-thin, meta-programming-powered predicate library for Rails.
|
|
14
|
+
#
|
|
15
|
+
# Features::
|
|
16
|
+
# * Pure boolean logic (true/false)
|
|
17
|
+
# * Meta-programming DSL with `method_missing`
|
|
18
|
+
# * Automatic memoization and caching
|
|
19
|
+
# * Rails auto-detection (validations, scopes, associations)
|
|
20
|
+
# * Section-based predicate grouping
|
|
21
|
+
# * English-like helper methods (`is_present?`, `is_blank?`, `not?`)
|
|
22
|
+
# * Performance monitoring and statistics
|
|
23
|
+
# * Model auto-definition with `method_missing`
|
|
24
|
+
#
|
|
25
|
+
# Usage::
|
|
26
|
+
# Predicate.define(:assessment) do
|
|
27
|
+
# has_media { |s| is_present?(s[:media_attachment_ids]) }
|
|
28
|
+
# wizard_complete { |s| has_media(s) && has_title(s) }
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
# @author Nauman Tariq
|
|
32
|
+
# @version 1.0.0
|
|
33
|
+
|
|
34
|
+
module Predicate
|
|
35
|
+
# Custom exception for predicate-related errors
|
|
36
|
+
class InvalidPredicate < StandardError; end
|
|
37
|
+
|
|
38
|
+
# Main entry point for defining predicates
|
|
39
|
+
# @param entity [Symbol, String] The entity name (e.g., :assessment, :sale)
|
|
40
|
+
# @param cache_ttl [Numeric, nil] Cache time-to-live in seconds (nil = no expiration)
|
|
41
|
+
# @param thread_safe [Boolean] Enable thread-safe cache operations (default: false)
|
|
42
|
+
# @param block [Proc] Block containing predicate definitions
|
|
43
|
+
# @return [Predicate::Core] The predicate instance
|
|
44
|
+
# @example
|
|
45
|
+
# Predicate.define(:assessment) do
|
|
46
|
+
# has_media { |s| is_present?(s[:media_attachment_ids]) }
|
|
47
|
+
# wizard_complete { |s| has_media(s) && has_title(s) }
|
|
48
|
+
# end
|
|
49
|
+
# @example With TTL
|
|
50
|
+
# Predicate.define(:assessment, cache_ttl: 5.minutes) do
|
|
51
|
+
# has_media { |s| is_present?(s[:media_attachment_ids]) }
|
|
52
|
+
# end
|
|
53
|
+
# @example With thread-safety
|
|
54
|
+
# Predicate.define(:assessment, thread_safe: true, cache_ttl: 5.minutes) do
|
|
55
|
+
# has_media { |s| is_present?(s[:media_attachment_ids]) }
|
|
56
|
+
# end
|
|
57
|
+
def self.define(entity, cache_ttl: nil, thread_safe: false, &block)
|
|
58
|
+
builder = DSL::Builder.new(entity, nil, cache_ttl: cache_ttl, thread_safe: thread_safe)
|
|
59
|
+
builder.instance_eval(&block)
|
|
60
|
+
core = builder.build
|
|
61
|
+
Registry.register(entity, core)
|
|
62
|
+
core
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Get predicate instance for a given entity
|
|
66
|
+
# @param entity [Symbol, String] The entity name
|
|
67
|
+
# @return [Predicate::Core] The predicate instance
|
|
68
|
+
# @raise [KeyError] If no predicate is defined for the entity
|
|
69
|
+
# @example
|
|
70
|
+
# assessment_predicates = Predicate.for(:assessment)
|
|
71
|
+
# assessment_predicates.call(:has_media, { media_attachment_ids: [1] })
|
|
72
|
+
def self.for(entity)
|
|
73
|
+
Registry.fetch(entity)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Clear all predicate caches across the registry
|
|
77
|
+
# @example
|
|
78
|
+
# Predicate.clear_cache!
|
|
79
|
+
def self.clear_cache!
|
|
80
|
+
Registry.clear_cache!
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Clear all registered predicates from the registry
|
|
84
|
+
# @example
|
|
85
|
+
# Predicate.clear_registry!
|
|
86
|
+
def self.clear_registry!
|
|
87
|
+
Registry.clear_registry!
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# REGISTRY
|
|
91
|
+
# Central registry for storing and managing predicate instances
|
|
92
|
+
#
|
|
93
|
+
# Responsibilities:
|
|
94
|
+
# * Store predicate instances by entity name
|
|
95
|
+
# * Provide lookup and retrieval methods
|
|
96
|
+
# * Manage cache invalidation across all predicates
|
|
97
|
+
# * Track registry statistics and performance metrics
|
|
98
|
+
# * Handle predicate lifecycle management
|
|
99
|
+
#
|
|
100
|
+
# Usage:
|
|
101
|
+
# registry = Predicate::Registry.new
|
|
102
|
+
# registry.register(:assessment, predicate_instance)
|
|
103
|
+
# predicate = registry.fetch(:assessment)
|
|
104
|
+
# stats = registry.stats
|
|
105
|
+
class Registry
|
|
106
|
+
def initialize
|
|
107
|
+
@predicates = {}
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Register a predicate instance for an entity
|
|
111
|
+
# @param entity [Symbol, String] The entity name
|
|
112
|
+
# @param predicate_instance [Predicate::Core] The predicate instance
|
|
113
|
+
def register(entity, predicate_instance)
|
|
114
|
+
@predicates[entity.to_sym] = predicate_instance
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Fetch a predicate instance for an entity
|
|
118
|
+
# @param entity [Symbol, String] The entity name
|
|
119
|
+
# @return [Predicate::Core] The predicate instance
|
|
120
|
+
# @raise [KeyError] If no predicate is defined for the entity
|
|
121
|
+
def fetch(entity)
|
|
122
|
+
@predicates.fetch(entity.to_sym) { raise KeyError, "No predicate for #{entity}" }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Unregister a predicate instance for an entity
|
|
126
|
+
# @param entity [Symbol, String] The entity name
|
|
127
|
+
# @return [Predicate::Core, nil] The removed predicate instance or nil
|
|
128
|
+
def unregister(entity)
|
|
129
|
+
@predicates.delete(entity.to_sym)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Class method to register a predicate instance
|
|
133
|
+
# @param entity [Symbol, String] The entity name
|
|
134
|
+
# @param predicate_instance [Predicate::Core] The predicate instance
|
|
135
|
+
def self.register(entity, predicate_instance)
|
|
136
|
+
Predicate.registry.register(entity, predicate_instance)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Class method to fetch a predicate instance
|
|
140
|
+
# @param entity [Symbol, String] The entity name
|
|
141
|
+
# @return [Predicate::Core] The predicate instance
|
|
142
|
+
def self.fetch(entity)
|
|
143
|
+
Predicate.registry.fetch(entity)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Class method to unregister a predicate instance
|
|
147
|
+
# @param entity [Symbol, String] The entity name
|
|
148
|
+
# @return [Predicate::Core, nil] The removed predicate instance or nil
|
|
149
|
+
def self.unregister(entity)
|
|
150
|
+
Predicate.registry.unregister(entity)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Clear caches for all registered predicate instances
|
|
154
|
+
def self.clear_cache!
|
|
155
|
+
Predicate.registry.instance_variable_get(:@predicates).each_value(&:clear_cache!)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Clear all registered predicates from the registry
|
|
159
|
+
def self.clear_registry!
|
|
160
|
+
Predicate.registry.instance_variable_set(:@predicates, {})
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Default registry instance
|
|
165
|
+
def self.registry
|
|
166
|
+
@registry ||= Registry.new
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Include combinators in DSL after all files are loaded
|
|
170
|
+
DSL::Builder.include(Combinators::Helpers)
|
|
171
|
+
DSL::SectionBuilder.include(Combinators::Helpers)
|
|
172
|
+
|
|
173
|
+
# PERFORMANCE MONITORING
|
|
174
|
+
# Performance tracking and monitoring utilities for predicates
|
|
175
|
+
#
|
|
176
|
+
# Features:
|
|
177
|
+
# * Track predicate execution times
|
|
178
|
+
# * Monitor cache hit/miss ratios
|
|
179
|
+
# * Collect performance statistics
|
|
180
|
+
# * Rails cache integration for metrics storage
|
|
181
|
+
# * Performance bottleneck identification
|
|
182
|
+
#
|
|
183
|
+
# Usage:
|
|
184
|
+
# Predicate::Performance.track(:has_media, 0.001)
|
|
185
|
+
# stats = Predicate::Performance.stats(:has_media)
|
|
186
|
+
module Performance
|
|
187
|
+
@stats = {}
|
|
188
|
+
@mutex = Mutex.new
|
|
189
|
+
|
|
190
|
+
# Track predicate execution time
|
|
191
|
+
# @param predicate_name [Symbol] The predicate name
|
|
192
|
+
# @param duration [Float] Execution time in seconds
|
|
193
|
+
# @example
|
|
194
|
+
# Predicate::Performance.track(:has_media, 0.001)
|
|
195
|
+
def self.track(predicate_name, duration)
|
|
196
|
+
@mutex.synchronize do
|
|
197
|
+
@stats[predicate_name] ||= {
|
|
198
|
+
total_calls: 0,
|
|
199
|
+
total_time: 0.0,
|
|
200
|
+
avg_time: 0.0,
|
|
201
|
+
cache_hits: 0,
|
|
202
|
+
cache_misses: 0
|
|
203
|
+
}
|
|
204
|
+
stats = @stats[predicate_name]
|
|
205
|
+
stats[:total_calls] += 1
|
|
206
|
+
stats[:total_time] += duration
|
|
207
|
+
stats[:avg_time] = stats[:total_time] / stats[:total_calls]
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Log if Rails is available
|
|
211
|
+
if defined?(Rails) && Rails.respond_to?(:logger)
|
|
212
|
+
Rails.logger.debug { "Predicate #{predicate_name} executed in #{duration} seconds" }
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Track cache hit
|
|
217
|
+
# @param predicate_name [Symbol] The predicate name
|
|
218
|
+
def self.track_cache_hit(predicate_name)
|
|
219
|
+
@mutex.synchronize do
|
|
220
|
+
@stats[predicate_name] ||= {
|
|
221
|
+
total_calls: 0,
|
|
222
|
+
total_time: 0.0,
|
|
223
|
+
avg_time: 0.0,
|
|
224
|
+
cache_hits: 0,
|
|
225
|
+
cache_misses: 0
|
|
226
|
+
}
|
|
227
|
+
@stats[predicate_name][:cache_hits] += 1
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
if defined?(Rails) && Rails.respond_to?(:logger)
|
|
231
|
+
Rails.logger.debug { "Predicate #{predicate_name} cache hit" }
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# Track cache miss
|
|
236
|
+
# @param predicate_name [Symbol] The predicate name
|
|
237
|
+
def self.track_cache_miss(predicate_name)
|
|
238
|
+
@mutex.synchronize do
|
|
239
|
+
@stats[predicate_name] ||= {
|
|
240
|
+
total_calls: 0,
|
|
241
|
+
total_time: 0.0,
|
|
242
|
+
avg_time: 0.0,
|
|
243
|
+
cache_hits: 0,
|
|
244
|
+
cache_misses: 0
|
|
245
|
+
}
|
|
246
|
+
@stats[predicate_name][:cache_misses] += 1
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
if defined?(Rails) && Rails.respond_to?(:logger)
|
|
250
|
+
Rails.logger.debug { "Predicate #{predicate_name} cache miss" }
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# Get performance statistics for a predicate
|
|
255
|
+
# @param predicate_name [Symbol] The predicate name
|
|
256
|
+
# @return [Hash] Performance statistics (e.g., total_calls, total_time, avg_time)
|
|
257
|
+
def self.stats(predicate_name)
|
|
258
|
+
@mutex.synchronize do
|
|
259
|
+
@stats[predicate_name] || {
|
|
260
|
+
total_calls: 0,
|
|
261
|
+
total_time: 0.0,
|
|
262
|
+
avg_time: 0.0,
|
|
263
|
+
cache_hits: 0,
|
|
264
|
+
cache_misses: 0
|
|
265
|
+
}
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Reset statistics (useful for testing)
|
|
270
|
+
def self.reset!
|
|
271
|
+
@mutex.synchronize do
|
|
272
|
+
@stats = {}
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Load Railtie if Rails is present
|
|
279
|
+
require_relative 'predicate/railtie' if defined?(Rails::Railtie)
|
metadata
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: predicate-rb
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Nauman Tariq
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: json
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: |
|
|
27
|
+
A lightweight library for defining and evaluating boolean predicates with
|
|
28
|
+
automatic caching, combinators, and optional Rails model integration.
|
|
29
|
+
|
|
30
|
+
Features:
|
|
31
|
+
- Pure boolean predicates with meta-programming DSL
|
|
32
|
+
- Automatic memoization and caching with SHA256-based keys
|
|
33
|
+
- Logical combinators (AND, OR, NOT, conditional)
|
|
34
|
+
- Section-based predicate grouping
|
|
35
|
+
- Rails auto-detection and model integration
|
|
36
|
+
- Performance monitoring and statistics
|
|
37
|
+
- TTL cache expiration
|
|
38
|
+
- Thread-safe operations
|
|
39
|
+
email:
|
|
40
|
+
- hello@nauman.one
|
|
41
|
+
executables: []
|
|
42
|
+
extensions: []
|
|
43
|
+
extra_rdoc_files: []
|
|
44
|
+
files:
|
|
45
|
+
- CHANGELOG.md
|
|
46
|
+
- EXAMPLES.md
|
|
47
|
+
- LICENSE.txt
|
|
48
|
+
- README.md
|
|
49
|
+
- lib/predicate-rb.rb
|
|
50
|
+
- lib/predicate.rb
|
|
51
|
+
- lib/predicate/combinators.rb
|
|
52
|
+
- lib/predicate/core.rb
|
|
53
|
+
- lib/predicate/dsl.rb
|
|
54
|
+
- lib/predicate/model_integration.rb
|
|
55
|
+
- lib/predicate/railtie.rb
|
|
56
|
+
- lib/predicate/util.rb
|
|
57
|
+
- lib/predicate/validation.rb
|
|
58
|
+
- lib/predicate/version.rb
|
|
59
|
+
homepage: https://github.com/StockLive-Group/predicate
|
|
60
|
+
licenses:
|
|
61
|
+
- MIT
|
|
62
|
+
metadata:
|
|
63
|
+
homepage_uri: https://github.com/StockLive-Group/predicate
|
|
64
|
+
source_code_uri: https://github.com/StockLive-Group/predicate
|
|
65
|
+
changelog_uri: https://github.com/StockLive-Group/predicate/blob/main/CHANGELOG.md
|
|
66
|
+
bug_tracker_uri: https://github.com/StockLive-Group/predicate/issues
|
|
67
|
+
documentation_uri: https://github.com/StockLive-Group/predicate/blob/main/README.md
|
|
68
|
+
rdoc_options: []
|
|
69
|
+
require_paths:
|
|
70
|
+
- lib
|
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 3.0.0
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
requirements: []
|
|
82
|
+
rubygems_version: 4.0.20
|
|
83
|
+
specification_version: 4
|
|
84
|
+
summary: Boolean predicate DSL with caching for Ruby/Rails
|
|
85
|
+
test_files: []
|