mongory 0.9.0-aarch64-linux → 2.0.0-aarch64-linux

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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -0
  3. data/README.md +121 -422
  4. data/SUBMODULE_INTEGRATION.md +2 -2
  5. data/docs/clang_bridge.md +28 -59
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -10
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  14. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  15. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  16. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  20. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +69 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +15 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  26. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +167 -31
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +7 -1
  28. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  30. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +20 -6
  31. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  33. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  34. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +6 -5
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  39. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +120 -60
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  41. data/ext/mongory_ext/mongory_ext.c +2 -2
  42. data/ext/mongory_ext/mongory_ext_internal.h +2 -2
  43. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +157 -23
  44. data/lib/core/2.6/mongory_ext.so +0 -0
  45. data/lib/core/2.7/mongory_ext.so +0 -0
  46. data/lib/core/3.0/mongory_ext.so +0 -0
  47. data/lib/core/3.1/mongory_ext.so +0 -0
  48. data/lib/core/3.2/mongory_ext.so +0 -0
  49. data/lib/core/3.3/mongory_ext.so +0 -0
  50. data/lib/core/3.4/mongory_ext.so +0 -0
  51. data/lib/core/4.0/mongory_ext.so +0 -0
  52. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  53. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  54. data/lib/mongory/converters/converted.rb +28 -6
  55. data/lib/mongory/converters/key_converter.rb +2 -0
  56. data/lib/mongory/matcher.rb +63 -0
  57. data/lib/mongory/matchers/abstract_matcher.rb +18 -164
  58. data/lib/mongory/matchers.rb +68 -84
  59. data/lib/mongory/query_builder.rb +30 -41
  60. data/lib/mongory/utils/tree_renderer.rb +108 -0
  61. data/lib/mongory/utils.rb +2 -67
  62. data/lib/mongory/version.rb +1 -1
  63. data/lib/mongory.rb +9 -59
  64. data/script/bench.rb +54 -0
  65. metadata +13 -37
  66. data/lib/mongory/c_matcher.rb +0 -57
  67. data/lib/mongory/c_query_builder.rb +0 -44
  68. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -124
  69. data/lib/mongory/matchers/and_matcher.rb +0 -72
  70. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  71. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  72. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  73. data/lib/mongory/matchers/every_matcher.rb +0 -56
  74. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  75. data/lib/mongory/matchers/field_matcher.rb +0 -160
  76. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  77. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  78. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -79
  79. data/lib/mongory/matchers/in_matcher.rb +0 -84
  80. data/lib/mongory/matchers/literal_matcher.rb +0 -121
  81. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  84. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  85. data/lib/mongory/matchers/not_matcher.rb +0 -40
  86. data/lib/mongory/matchers/or_matcher.rb +0 -68
  87. data/lib/mongory/matchers/present_matcher.rb +0 -55
  88. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  89. data/lib/mongory/matchers/size_matcher.rb +0 -54
  90. data/lib/mongory/query_matcher.rb +0 -93
  91. data/lib/mongory/rails.rb +0 -15
  92. data/lib/mongory/utils/debugger.rb +0 -125
  93. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # InMatcher implements the `$in` operator.
6
- #
7
- # It checks whether the record matches any value in the condition array.
8
- # If the record is an array, it succeeds if any item overlaps with the condition.
9
- # If the record is a single value (including `nil`), it matches if it is included in the condition.
10
- #
11
- # @example Match single value
12
- # matcher = InMatcher.build([1, 2, 3])
13
- # matcher.match?(2) #=> true
14
- # matcher.match?(5) #=> false
15
- #
16
- # @example Match nil
17
- # matcher = InMatcher.build([nil])
18
- # matcher.match?(nil) #=> true
19
- #
20
- # @example Match with array
21
- # matcher = InMatcher.build([2, 4])
22
- # matcher.match?([1, 2, 3]) #=> true
23
- # matcher.match?([5, 6]) #=> false
24
- #
25
- # @see AbstractMatcher
26
- class InMatcher < AbstractMatcher
27
- def self.build(condition, context: Context.new)
28
- return super unless condition.is_a?(Range)
29
-
30
- end_op = condition.exclude_end? ? '$lt' : '$lte'
31
- head, tail = [condition.first, condition.last].sort
32
- AndMatcher.build([{ '$gte' => head }, { end_op => tail }], context: context)
33
- end
34
-
35
- # Creates a raw Proc that performs the in-matching operation.
36
- # The Proc checks if any element of the record is in the condition array.
37
- #
38
- # @return [Proc] a Proc that performs the in-matching operation
39
- def raw_proc
40
- condition = build_condition_set
41
-
42
- Proc.new do |record|
43
- if record.is_a?(Array)
44
- is_present?(condition & record)
45
- else
46
- condition.include?(record)
47
- end
48
- end
49
- end
50
-
51
- def priority
52
- 1 + Math.log(@condition.size + 1, 1.5)
53
- end
54
-
55
- # Ensures the condition is an array or range.
56
- #
57
- # @raise [TypeError] if condition is not an array nor a range
58
- # @return [void]
59
- def check_validity!
60
- return if @condition.is_a?(Array)
61
-
62
- raise TypeError, '$in needs an array or range'
63
- end
64
-
65
- private
66
-
67
- def build_condition_set
68
- Set.new(@condition).tap do |set|
69
- @condition.each do |item|
70
- if item.is_a?(Integer)
71
- float_val = item.to_f
72
- set.add(float_val) if float_val == item
73
- elsif item.is_a?(Float) && item.finite?
74
- int_val = item.to_i
75
- set.add(int_val) if int_val == item
76
- end
77
- end
78
- end
79
- end
80
- end
81
-
82
- register(:in, '$in', InMatcher)
83
- end
84
- end
@@ -1,121 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # LiteralMatcher handles direct value comparison with special array handling.
6
- #
7
- # This matcher is used when a condition is a literal value (not an operator).
8
- # It handles both direct equality comparison and array-record scenarios.
9
- #
10
- # For array records:
11
- # - Uses ArrayRecordMatcher to check if any element matches
12
- # For non-array records:
13
- # - Uses appropriate matcher based on condition type (Hash, Regexp, nil, etc.)
14
- #
15
- # @example Basic equality matching
16
- # matcher = LiteralMatcher.build(42)
17
- # matcher.match?(42) #=> true
18
- # matcher.match?([42, 43]) #=> true (array contains 42)
19
- #
20
- # @example Regexp matching
21
- # matcher = LiteralMatcher.build(/foo/)
22
- # matcher.match?("foo") #=> true
23
- # matcher.match?(["foobar"]) #=> true
24
- #
25
- # @example Hash condition matching
26
- # matcher = LiteralMatcher.build({ '$gt' => 10 })
27
- # matcher.match?(15) #=> true
28
- # matcher.match?([5, 15]) #=> true
29
- #
30
- # @see AbstractMatcher
31
- # @see ArrayRecordMatcher
32
- class LiteralMatcher < AbstractMatcher
33
- # Creates a raw Proc that performs the literal matching operation.
34
- # The Proc handles both array and non-array records appropriately.
35
- #
36
- # @return [Proc] a Proc that performs the literal matching operation
37
- def raw_proc
38
- array_record_proc = nil
39
- dispatched_proc = dispatched_matcher.to_proc
40
-
41
- Proc.new do |record|
42
- if record.is_a?(Array)
43
- array_record_proc ||= array_record_matcher.to_proc
44
- array_record_proc.call(record)
45
- else
46
- dispatched_proc.call(record)
47
- end
48
- end
49
- end
50
-
51
- def priority
52
- 1 + dispatched_matcher.priority
53
- end
54
-
55
- # Selects and returns the appropriate matcher instance for a given literal condition.
56
- #
57
- # This method analyzes the type of the raw condition (e.g., Hash, Regexp, nil)
58
- # and returns a dedicated matcher instance accordingly:
59
- #
60
- # - Hash → dispatches to `HashConditionMatcher`
61
- # - Regexp → dispatches to `RegexMatcher`
62
- # - nil → dispatches to an `OrMatcher` that emulates MongoDB's `{ field: nil }` behavior
63
- #
64
- # For all other literal types, this method returns `EqMatcher`, and fallback equality matching will be used.
65
- #
66
- # This matcher is cached after the first invocation using `define_instance_cache_method`
67
- # to avoid unnecessary re-instantiation.
68
- #
69
- # @see Mongory::Matchers::HashConditionMatcher
70
- # @see Mongory::Matchers::RegexMatcher
71
- # @see Mongory::Matchers::OrMatcher
72
- # @see Mongory::Matchers::EqMatcher
73
- # @return [AbstractMatcher] the matcher used for non-array literal values
74
- # @!method dispatched_matcher
75
- define_matcher(:dispatched) do
76
- case @condition
77
- when Hash
78
- HashConditionMatcher.build(@condition, context: @context)
79
- when Regexp
80
- RegexMatcher.build(@condition, context: @context)
81
- when nil
82
- OrMatcher.build([
83
- { '$exists' => false },
84
- { '$eq' => nil }
85
- ], context: @context)
86
- else
87
- EqMatcher.build(@condition, context: @context)
88
- end
89
- end
90
-
91
- # Lazily defines the collection matcher for array records.
92
- #
93
- # @see ArrayRecordMatcher
94
- # @return [ArrayRecordMatcher] the matcher used to match array-type records
95
- # @!method array_record_matcher
96
- define_matcher(:array_record) do
97
- ArrayRecordMatcher.build(@condition, context: @context)
98
- end
99
-
100
- # Validates the nested condition matcher, if applicable.
101
- #
102
- # @return [void]
103
- def check_validity!
104
- dispatched_matcher.check_validity!
105
- end
106
-
107
- # Outputs the matcher tree by selecting either collection or condition matcher.
108
- # Delegates `render_tree` to whichever submatcher was active.
109
- #
110
- # @param prefix [String] the prefix string for tree rendering
111
- # @param is_last [Boolean] whether this is the last node in the tree
112
- # @return [void]
113
- def render_tree(prefix = '', is_last: true)
114
- super
115
-
116
- target_matcher = @array_record_matcher || dispatched_matcher
117
- target_matcher.render_tree("#{prefix}#{is_last ? ' ' : '│ '}", is_last: true)
118
- end
119
- end
120
- end
121
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # LtMatcher implements the `$lt` (less than) operator.
6
- #
7
- # It returns true if the record is strictly less than the condition value.
8
- #
9
- # This matcher inherits from AbstractMatcher and uses the `<` operator.
10
- #
11
- # @example
12
- # matcher = LtMatcher.build(10)
13
- # matcher.match?(9) #=> true
14
- # matcher.match?(10) #=> false
15
- # matcher.match?(11) #=> false
16
- #
17
- # @see AbstractMatcher
18
- class LtMatcher < AbstractMatcher
19
- # Creates a raw Proc that performs the less-than comparison.
20
- # The Proc uses the `<` operator to compare values.
21
- #
22
- # @return [Proc] A proc that performs less-than comparison with error handling
23
- # @note The proc includes error handling for invalid comparisons
24
- def raw_proc
25
- condition = @condition
26
-
27
- Proc.new do |record|
28
- record < condition
29
- rescue StandardError
30
- false
31
- end
32
- end
33
-
34
- def priority
35
- 3
36
- end
37
- end
38
-
39
- register(:lt, '$lt', LtMatcher)
40
- end
41
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # LteMatcher implements the `$lte` (less than or equal to) operator.
6
- #
7
- # It returns true if the record is less than or equal to the condition value.
8
- #
9
- # This matcher inherits from AbstractMatcher and uses the `<=` operator.
10
- #
11
- # @example
12
- # matcher = LteMatcher.build(10)
13
- # matcher.match?(9) #=> true
14
- # matcher.match?(10) #=> true
15
- # matcher.match?(11) #=> false
16
- #
17
- # @see AbstractMatcher
18
- class LteMatcher < AbstractMatcher
19
- # Creates a raw Proc that performs the less-than-or-equal comparison.
20
- # The Proc uses the `<=` operator to compare values.
21
- #
22
- # @return [Proc] A proc that performs less-than-or-equal comparison with error handling
23
- # @note The proc includes error handling for invalid comparisons
24
- def raw_proc
25
- condition = @condition
26
-
27
- Proc.new do |record|
28
- record <= condition
29
- rescue StandardError
30
- false
31
- end
32
- end
33
-
34
- def priority
35
- 3
36
- end
37
- end
38
-
39
- register(:lte, '$lte', LteMatcher)
40
- end
41
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # NeMatcher implements the `$ne` (not equal) operator.
6
- #
7
- # It returns true if the record is *not equal* to the condition.
8
- # If the record is an `Array`, it returns false if either the array itself
9
- # equals the condition or any element in the array equals the condition.
10
- #
11
- # @example
12
- # matcher = NeMatcher.build(42)
13
- # matcher.match?(41) #=> true
14
- # matcher.match?(42) #=> false
15
- # matcher.match?([10, 42]) #=> false
16
- # matcher.match?([10, 20]) #=> true
17
- #
18
- # @see AbstractMatcher
19
- class NeMatcher < AbstractMatcher
20
- # Creates a raw Proc that performs the not-equal comparison.
21
- # The Proc uses the `!=` operator to compare values.
22
- #
23
- # @return [Proc] A proc that performs not-equal comparison
24
- def raw_proc
25
- condition = @condition
26
-
27
- Proc.new do |record|
28
- record != condition && !(record.is_a?(Array) && record.include?(condition))
29
- end
30
- end
31
-
32
- def priority
33
- 1
34
- end
35
- end
36
-
37
- register(:ne, '$ne', NeMatcher)
38
- end
39
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # NinMatcher implements the `$nin` (not in) operator.
6
- #
7
- # It succeeds only if the record does not match any value in the condition array.
8
- # If the record is an array, it fails if any element overlaps with the condition.
9
- # If the record is a single value (including `nil`), it fails if it is included in the condition.
10
- #
11
- # @example Match single value
12
- # matcher = NinMatcher.build([1, 2, 3])
13
- # matcher.match?(4) #=> true
14
- # matcher.match?(2) #=> false
15
- #
16
- # @example Match nil
17
- # matcher = NinMatcher.build([nil])
18
- # matcher.match?(nil) #=> false
19
- #
20
- # @example Match with array
21
- # matcher = NinMatcher.build([2, 4])
22
- # matcher.match?([1, 3, 5]) #=> true
23
- # matcher.match?([4, 5]) #=> false
24
- #
25
- # @see AbstractMatcher
26
- class NinMatcher < InMatcher
27
- def self.build(condition, context: Context.new)
28
- return super unless condition.is_a?(Range)
29
-
30
- end_op = condition.exclude_end? ? '$gte' : '$gt'
31
- head, tail = [condition.first, condition.last].sort
32
- OrMatcher.build([{ '$lt' => head }, { end_op => tail }], context: context)
33
- end
34
-
35
- # Creates a raw Proc that performs the not-in matching operation.
36
- # The Proc checks if the record has no elements in common with the condition array.
37
- #
38
- # @return [Proc] A proc that performs not-in matching
39
- def raw_proc
40
- condition = build_condition_set
41
-
42
- Proc.new do |record|
43
- if record.is_a?(Array)
44
- is_blank?(condition & record)
45
- else
46
- !condition.include?(record)
47
- end
48
- end
49
- end
50
-
51
- # Ensures the condition is a valid array or range.
52
- #
53
- # @raise [TypeError] if the condition is not an array nor a range
54
- # @return [void]
55
- def check_validity!
56
- return if @condition.is_a?(Array)
57
-
58
- raise TypeError, '$nin needs an array or range'
59
- end
60
- end
61
-
62
- register(:nin, '$nin', NinMatcher)
63
- end
64
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # NotMatcher implements the `$not` logical operator.
6
- #
7
- # It returns true if the wrapped matcher fails, effectively inverting the result.
8
- #
9
- # It delegates to LiteralMatcher and simply negates the outcome.
10
- #
11
- # This allows constructs like:
12
- # { age: { :$not => { :$gte => 30 } } }
13
- #
14
- # @example
15
- # matcher = NotMatcher.build({ :$gte => 10 })
16
- # matcher.match?(5) #=> true
17
- # matcher.match?(15) #=> false
18
- #
19
- # @see LiteralMatcher
20
- class NotMatcher < LiteralMatcher
21
- # Creates a raw Proc that performs the not-matching operation.
22
- # The Proc inverts the result of the wrapped matcher.
23
- #
24
- # @return [Proc] A proc that performs not-matching
25
- def raw_proc
26
- super_proc = super
27
-
28
- Proc.new do |record|
29
- !super_proc.call(record)
30
- end
31
- end
32
-
33
- def priority
34
- 1 + super
35
- end
36
- end
37
-
38
- register(:not, '$not', NotMatcher)
39
- end
40
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # OrMatcher implements the `$or` logical operator.
6
- #
7
- # It evaluates an array of subconditions and returns true
8
- # if *any one* of them matches. For empty conditions, it returns false
9
- # (using FALSE_PROC).
10
- #
11
- # Each subcondition is handled by a HashConditionMatcher with conversion disabled,
12
- # since the parent matcher already manages data conversion.
13
- #
14
- # This matcher inherits submatcher dispatch and evaluation logic
15
- # from AbstractMultiMatcher.
16
- #
17
- # @example
18
- # matcher = OrMatcher.build([
19
- # { age: { :$lt => 18 } },
20
- # { admin: true }
21
- # ])
22
- # matcher.match?(record) #=> true if either condition matches
23
- #
24
- # @example Empty conditions
25
- # matcher = OrMatcher.build([])
26
- # matcher.match?(record) #=> false (uses FALSE_PROC)
27
- #
28
- # @see AbstractMultiMatcher
29
- class OrMatcher < AbstractMultiMatcher
30
- enable_unwrap!
31
-
32
- # Creates a raw Proc that performs the or-matching operation.
33
- # The Proc combines all submatcher Procs and returns true if any match.
34
- # For empty conditions, returns FALSE_PROC.
35
- #
36
- # @return [Proc] a Proc that performs the or-matching operation
37
- def raw_proc
38
- combine_procs_with_or(*matchers.map(&:to_proc))
39
- end
40
-
41
- # Builds an array of matchers from the subconditions.
42
- # Each subcondition is wrapped in a HashConditionMatcher.
43
- #
44
- # @return [Array<AbstractMatcher>] array of submatchers
45
- define_instance_cache_method(:matchers) do
46
- @condition.map do |condition|
47
- HashConditionMatcher.build(condition, context: @context)
48
- end.sort_by(&:priority)
49
- end
50
-
51
- # Ensures the condition is an array of hashes.
52
- #
53
- # @raise [Mongory::TypeError] if not valid
54
- # @return [void]
55
- def check_validity!
56
- raise TypeError, '$or needs an array' unless @condition.is_a?(Array)
57
-
58
- @condition.each do |sub_condition|
59
- raise TypeError, '$or needs an array of hash' unless sub_condition.is_a?(Hash)
60
- end
61
-
62
- super
63
- end
64
- end
65
-
66
- register(:or, '$or', OrMatcher)
67
- end
68
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # PresentMatcher implements the `$present` operator.
6
- #
7
- # It returns true if the record value is considered "present"
8
- # (i.e., not nil, not empty, not KEY_NOT_FOUND), and matches
9
- # the expected boolean condition.
10
- #
11
- # This is similar to `$exists`, but evaluates truthiness
12
- # of the value instead of mere existence.
13
- #
14
- # @example
15
- # matcher = PresentMatcher.build(true)
16
- # matcher.match?('hello') #=> true
17
- # matcher.match?(nil) #=> false
18
- # matcher.match?([]) #=> false
19
- #
20
- # matcher = PresentMatcher.build(false)
21
- # matcher.match?(nil) #=> true
22
- #
23
- # @see AbstractMatcher
24
- class PresentMatcher < AbstractMatcher
25
- # Creates a raw Proc that performs the presence check.
26
- # The Proc checks if the record's presence matches the condition.
27
- #
28
- # @return [Proc] A proc that performs presence check
29
- def raw_proc
30
- condition = @condition
31
-
32
- Proc.new do |record|
33
- record = nil if record == KEY_NOT_FOUND
34
- is_present?(record) == condition
35
- end
36
- end
37
-
38
- def priority
39
- 2
40
- end
41
-
42
- # Ensures that the condition value is a boolean.
43
- #
44
- # @raise [TypeError] if condition is not true or false
45
- # @return [void]
46
- def check_validity!
47
- return if [true, false].include?(@condition)
48
-
49
- raise TypeError, '$present needs a boolean'
50
- end
51
- end
52
-
53
- register(:present, '$present', PresentMatcher)
54
- end
55
- end
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # RegexMatcher implements the `$regex` operator and also handles raw Regexp values.
6
- #
7
- # This matcher checks whether a string record matches a regular expression.
8
- # It supports both:
9
- # - Explicit queries using `:field.regex => /pattern/i`
10
- # - Implicit literal Regexp values like `{ field: /pattern/i }`
11
- #
12
- # If a string is provided instead of a Regexp, it will be converted via `Regexp.new(...)`.
13
- # This ensures consistent behavior for queries like `:field.regex => "foo"` and `:field.regex => /foo/`.
14
- #
15
- # @example Basic regex matching
16
- # matcher = RegexMatcher.build(/^foo/)
17
- # matcher.match?('foobar') #=> true
18
- # matcher.match?('barfoo') #=> false
19
- #
20
- # @example Case-insensitive matching
21
- # matcher = RegexMatcher.build(/admin/i)
22
- # matcher.match?("ADMIN") #=> true
23
- #
24
- # @example String pattern
25
- # matcher = RegexMatcher.build("^foo")
26
- # matcher.match?("foobar") #=> true
27
- #
28
- # @example Non-string input
29
- # matcher = RegexMatcher.build(/\d+/)
30
- # matcher.match?(123) #=> false (not a string)
31
- #
32
- # @see AbstractMatcher
33
- class RegexMatcher < AbstractMatcher
34
- # MongoDB `$options` flags mapped to Ruby. Ruby's `^`/`$` always match at
35
- # line boundaries, so `m` needs no flag; Ruby's MULTILINE is MongoDB's `s`.
36
- OPTION_FLAGS = {
37
- 'i' => Regexp::IGNORECASE,
38
- 'm' => 0,
39
- 's' => Regexp::MULTILINE
40
- }.freeze
41
-
42
- # Compiles a `$regex` pattern string with MongoDB `$options` flags.
43
- #
44
- # @param pattern [String, Regexp, Object] the `$regex` value
45
- # @param options [Object] the `$options` value
46
- # @return [Regexp, Object] the compiled Regexp, or a non-string pattern
47
- # unchanged so that check_validity! reports it
48
- # @raise [TypeError] if options is invalid or pattern is already a Regexp
49
- def self.apply_options(pattern, options)
50
- raise TypeError, '$options needs a string' unless options.is_a?(String)
51
-
52
- flags = options.each_char.reduce(0) do |result, char|
53
- raise TypeError, "unsupported $options flag #{char.inspect}" unless OPTION_FLAGS.key?(char)
54
-
55
- result | OPTION_FLAGS[char]
56
- end
57
- raise TypeError, '$options cannot modify a Regexp' if pattern.is_a?(Regexp)
58
- return pattern unless pattern.is_a?(String)
59
-
60
- Regexp.new(pattern, flags)
61
- end
62
-
63
- # Initializes the matcher with a regex pattern.
64
- # Converts string patterns to Regexp objects.
65
- #
66
- # @param condition [String, Regexp] the regex pattern to match against
67
- # @param context [Context] the query context
68
- # @raise [TypeError] if condition is not a string or Regexp
69
- def initialize(condition, context: Context.new)
70
- super
71
- @condition = Regexp.new(condition) if condition.is_a?(String)
72
- end
73
-
74
- # Creates a raw Proc that performs the regex matching operation.
75
- # The Proc checks if the record is a string that matches the pattern.
76
- # Returns false for non-string inputs or if the match fails.
77
- #
78
- # @return [Proc] a Proc that performs regex matching
79
- def raw_proc
80
- condition = @condition
81
-
82
- Proc.new do |record|
83
- next false unless record.is_a?(String)
84
-
85
- record.match?(condition)
86
- rescue StandardError
87
- false
88
- end
89
- end
90
-
91
- def priority
92
- @condition.source.start_with?('^') ? 8 : 20
93
- end
94
-
95
- # Ensures the condition is a valid regex pattern (Regexp or String).
96
- #
97
- # @raise [TypeError] if condition is not a string or Regexp
98
- # @return [void]
99
- def check_validity!
100
- return if @condition.is_a?(Regexp)
101
- return if @condition.is_a?(String)
102
-
103
- raise TypeError, '$regex needs a Regexp or string'
104
- end
105
- end
106
-
107
- register(:regex, '$regex', RegexMatcher)
108
- end
109
- end