mongory 0.9.1 → 2.0.1

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 (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +115 -420
  4. data/SUBMODULE_INTEGRATION.md +7 -20
  5. data/docs/clang_bridge.md +28 -63
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -26
  9. data/ext/mongory_ext/extconf.rb +1 -1
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  14. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  15. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  16. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  17. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  18. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  20. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  22. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  23. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +68 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -0
  26. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +132 -29
  28. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -1
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  30. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  31. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -6
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  33. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  34. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +4 -4
  39. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +104 -60
  41. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  42. data/ext/mongory_ext/mongory_ext.c +2 -2
  43. data/ext/mongory_ext/mongory_ext_internal.h +2 -3
  44. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +145 -91
  45. data/ext/mongory_ext/rb_mongory_value.c +67 -0
  46. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  47. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  48. data/lib/mongory/converters/condition_converter.rb +1 -1
  49. data/lib/mongory/converters/converted.rb +28 -6
  50. data/lib/mongory/converters/key_converter.rb +2 -0
  51. data/lib/mongory/matcher.rb +50 -0
  52. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  53. data/lib/mongory/matchers.rb +68 -84
  54. data/lib/mongory/query_builder.rb +29 -42
  55. data/lib/mongory/utils/context.rb +10 -15
  56. data/lib/mongory/utils/tree_renderer.rb +98 -0
  57. data/lib/mongory/utils.rb +2 -67
  58. data/lib/mongory/version.rb +1 -1
  59. data/lib/mongory.rb +9 -59
  60. data/script/bench.rb +54 -0
  61. data/scripts/build_with_core.sh +0 -1
  62. metadata +13 -37
  63. data/lib/mongory/c_matcher.rb +0 -57
  64. data/lib/mongory/c_query_builder.rb +0 -44
  65. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  66. data/lib/mongory/matchers/and_matcher.rb +0 -73
  67. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  68. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  69. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  70. data/lib/mongory/matchers/every_matcher.rb +0 -56
  71. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  72. data/lib/mongory/matchers/field_matcher.rb +0 -160
  73. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  74. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  75. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  76. data/lib/mongory/matchers/in_matcher.rb +0 -84
  77. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  78. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  79. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  80. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  81. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  82. data/lib/mongory/matchers/not_matcher.rb +0 -40
  83. data/lib/mongory/matchers/or_matcher.rb +0 -69
  84. data/lib/mongory/matchers/present_matcher.rb +0 -55
  85. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  86. data/lib/mongory/matchers/size_matcher.rb +0 -54
  87. data/lib/mongory/query_matcher.rb +0 -93
  88. data/lib/mongory/rails.rb +0 -15
  89. data/lib/mongory/utils/debugger.rb +0 -125
  90. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -1,150 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # AbstractMultiMatcher is an abstract base class for matchers that operate
6
- # on multiple subconditions. It provides a generic match loop that applies
7
- # a logical operator (e.g., `all?`, `any?`) over a list of sub-matchers.
8
- #
9
- # Subclasses must define two methods:
10
- # - `#build_sub_matcher`: how to construct a matcher from each condition
11
- # - `#operator`: which enumerator method to use (e.g., :all?, :any?)
12
- #
13
- # Sub-matchers are cached using `define_instance_cache_method` to prevent
14
- # repeated construction.
15
- #
16
- # @abstract
17
- # @see AbstractMatcher
18
- class AbstractMultiMatcher < AbstractMatcher
19
- # A Proc that always returns true, used as a default for empty AND conditions
20
- # @return [Proc] A proc that always returns true
21
- TRUE_PROC = Proc.new { |_| true }.freeze
22
-
23
- # A Proc that always returns false, used as a default for empty OR conditions
24
- # @return [Proc] A proc that always returns false
25
- FALSE_PROC = Proc.new { |_| false }.freeze
26
-
27
- # Enables auto-unwrap logic.
28
- # When used, `.build` may unwrap to first matcher if only one is present.
29
- #
30
- # @private
31
- # @return [void]
32
- def self.enable_unwrap!
33
- @enable_unwrap = true
34
- singleton_class.alias_method :build, :build_or_unwrap
35
- end
36
-
37
- private_class_method :enable_unwrap!
38
-
39
- # Builds a matcher and conditionally unwraps it.
40
- # If unwrapping is enabled and there is only one submatcher,
41
- # returns that submatcher instead of the multi-matcher wrapper.
42
- #
43
- # @param args [Array] arguments passed to the constructor
44
- # @param context [Context] the query context
45
- # @return [AbstractMatcher] the constructed matcher or its unwrapped submatcher
46
- def self.build_or_unwrap(*args, context: Context.new)
47
- matcher = new(*args, context: context)
48
- return matcher unless @enable_unwrap
49
- return matcher unless matcher.matchers.count == 1
50
-
51
- unwrapped = matcher.matchers.first
52
- unwrapped.parent = nil if unwrapped.respond_to?(:parent=)
53
- unwrapped
54
- end
55
-
56
- # Recursively checks all submatchers for validity.
57
- # Raises an error if any submatcher is invalid.
58
- #
59
- # @raise [Mongory::TypeError] if any submatcher is invalid
60
- # @return [void]
61
- def check_validity!
62
- matchers.each(&:check_validity!)
63
- end
64
-
65
- # Overrides base render_tree to recursively print all submatchers.
66
- # Each child matcher will be displayed under this multi-matcher node.
67
- #
68
- # @param prefix [String] current line prefix for tree alignment
69
- # @param is_last [Boolean] whether this node is the last sibling
70
- # @return [void]
71
- def render_tree(prefix = '', is_last: true)
72
- super
73
-
74
- new_prefix = "#{prefix}#{is_last ? ' ' : '│ '}"
75
- last_index = matchers.count - 1
76
- matchers.each_with_index do |matcher, index|
77
- matcher.render_tree(new_prefix, is_last: index == last_index)
78
- end
79
- end
80
-
81
- def priority
82
- 1 + matchers.sum(&:priority)
83
- end
84
-
85
- # Re-sorts active child matchers after a lazy submatcher increases priority
86
- # and invalidates memoized procs up the tree.
87
- #
88
- # @return [void]
89
- def on_child_priority_changed
90
- resort_matchers!
91
- super
92
- end
93
-
94
- private
95
-
96
- def sort_and_adopt(children)
97
- children.each { |child| attach_child(child) }
98
- stable_sort_by_priority(children)
99
- end
100
-
101
- def resort_matchers!
102
- return unless instance_variable_defined?(:@matchers)
103
-
104
- @matchers = stable_sort_by_priority(@matchers)
105
- end
106
-
107
- def stable_sort_by_priority(children)
108
- children.each_with_index.sort_by { |child, idx| [child.priority, idx] }.map(&:first)
109
- end
110
-
111
- # Recursively combines multiple matcher procs with AND logic.
112
- # This method optimizes the combination of multiple matchers by building
113
- # a balanced tree of AND operations.
114
- #
115
- # @param left [Proc] The left matcher proc to combine
116
- # @param rest [Array<Proc>] The remaining matcher procs to combine
117
- # @return [Proc] A new proc that combines all matchers with AND logic
118
- # @example
119
- # combine_procs_with_and(proc1, proc2, proc3)
120
- # #=> proc { |record| proc1.call(record) && proc2.call(record) && proc3.call(record) }
121
- def combine_procs_with_and(left = TRUE_PROC, *rest)
122
- return left if rest.empty?
123
-
124
- right = combine_procs_with_and(*rest)
125
- Proc.new do |record|
126
- left.call(record) && right.call(record)
127
- end
128
- end
129
-
130
- # Recursively combines multiple matcher procs with OR logic.
131
- # This method optimizes the combination of multiple matchers by building
132
- # a balanced tree of OR operations.
133
- #
134
- # @param left [Proc] The left matcher proc to combine
135
- # @param rest [Array<Proc>] The remaining matcher procs to combine
136
- # @return [Proc] A new proc that combines all matchers with OR logic
137
- # @example
138
- # combine_procs_with_or(proc1, proc2, proc3)
139
- # #=> proc { |record| proc1.call(record) || proc2.call(record) || proc3.call(record) }
140
- def combine_procs_with_or(left = FALSE_PROC, *rest)
141
- return left if rest.empty?
142
-
143
- right = combine_procs_with_or(*rest)
144
- Proc.new do |record|
145
- left.call(record) || right.call(record)
146
- end
147
- end
148
- end
149
- end
150
- end
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # AndMatcher implements the `$and` logical operator.
6
- #
7
- # It evaluates an array of subconditions and returns true only if *all* of them match.
8
- # For empty conditions, it returns true (using TRUE_PROC), following MongoDB's behavior.
9
- #
10
- # Unlike other matchers, AndMatcher flattens the underlying matcher tree by
11
- # delegating each subcondition to a `HashConditionMatcher`, and further extracting
12
- # all inner matchers. Duplicate matchers are deduplicated by `uniq_key`.
13
- #
14
- # This allows the matcher trace (`.explain`) to render as a flat list of independent conditions.
15
- #
16
- # @example Basic usage
17
- # matcher = AndMatcher.build([
18
- # { age: { :$gte => 18 } },
19
- # { name: /foo/ }
20
- # ])
21
- # matcher.match?(record) #=> true if both match
22
- #
23
- # @example Empty conditions
24
- # matcher = AndMatcher.build([])
25
- # matcher.match?(record) #=> true (uses TRUE_PROC)
26
- #
27
- # @see AbstractMultiMatcher
28
- class AndMatcher < AbstractMultiMatcher
29
- # Constructs a HashConditionMatcher for each subcondition.
30
- # Conversion is disabled to avoid double-processing.
31
- enable_unwrap!
32
-
33
- # Creates a raw Proc that performs the AND operation.
34
- # The Proc combines all subcondition Procs and returns true only if all match.
35
- # For empty conditions, returns TRUE_PROC.
36
- #
37
- # @return [Proc] a Proc that performs the AND operation
38
- def raw_proc
39
- combine_procs_with_and(*matchers.map(&:to_proc))
40
- end
41
-
42
- # Returns the flattened list of all matchers from each subcondition.
43
- #
44
- # Each condition is passed to a HashConditionMatcher, then recursively flattened.
45
- # All matchers are then deduplicated using `uniq_key`.
46
- #
47
- # @return [Array<AbstractMatcher>] A flattened, deduplicated list of matchers
48
- # @see AbstractMatcher#uniq_key
49
- define_instance_cache_method(:matchers) do
50
- children = @condition.flat_map do |condition|
51
- HashConditionMatcher.new(condition, context: @context).matchers
52
- end.uniq(&:uniq_key)
53
- sort_and_adopt(children)
54
- end
55
-
56
- # Ensures the condition is an array of hashes.
57
- #
58
- # @raise [Mongory::TypeError] if not valid
59
- # @return [void]
60
- def check_validity!
61
- raise TypeError, '$and needs an array' unless @condition.is_a?(Array)
62
-
63
- @condition.each do |sub_condition|
64
- raise TypeError, '$and needs an array of hash' unless sub_condition.is_a?(Hash)
65
- end
66
-
67
- super
68
- end
69
- end
70
-
71
- register(:and, '$and', AndMatcher)
72
- end
73
- end
@@ -1,105 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # ArrayRecordMatcher handles matching against array-type records.
6
- #
7
- # This matcher is used when a field value is an array and needs to be matched
8
- # against a condition. It supports both exact array matching and element-wise
9
- # comparison through `$elemMatch`.
10
- #
11
- # For empty conditions, it returns false (using FALSE_PROC).
12
- #
13
- # @example Match exact array
14
- # matcher = ArrayRecordMatcher.build([1, 2, 3])
15
- # matcher.match?([1, 2, 3]) #=> true
16
- # matcher.match?([1, 2]) #=> false
17
- #
18
- # @example Match with hash condition
19
- # matcher = ArrayRecordMatcher.build({ '$gt' => 5 })
20
- # matcher.match?([3, 6, 9]) #=> true (6 and 9 match)
21
- # matcher.match?([1, 2, 3]) #=> false
22
- #
23
- # @example Empty conditions
24
- # matcher = ArrayRecordMatcher.build([])
25
- # matcher.match?(record) #=> false (uses FALSE_PROC)
26
- #
27
- # @see AbstractMultiMatcher
28
- class ArrayRecordMatcher < AbstractMultiMatcher
29
- enable_unwrap!
30
-
31
- # Creates a raw Proc that performs the array matching operation.
32
- # The Proc checks if any element in the array matches the condition.
33
- # For empty conditions, returns FALSE_PROC.
34
- #
35
- # @return [Proc] a Proc that performs the array matching operation
36
- def raw_proc
37
- combine_procs_with_or(*matchers.map(&:to_proc))
38
- end
39
-
40
- # Builds an array of matchers to evaluate the given condition against an array record.
41
- #
42
- # This method returns multiple matchers that will be evaluated using `:any?` logic:
43
- # - An equality matcher for exact array match
44
- # - A hash condition matcher if the condition is a hash
45
- # - An `$elemMatch` matcher for element-wise comparison
46
- #
47
- # @return [Array<AbstractMatcher>] An array of matcher instances
48
- define_instance_cache_method(:matchers) do
49
- result = []
50
- result << EqMatcher.build(@condition, context: @context) if @condition.is_a?(Array)
51
- result << case @condition
52
- when Hash
53
- HashConditionMatcher.build(parsed_condition, context: @context)
54
- when Regexp
55
- ElemMatchMatcher.build({ '$regex' => @condition }, context: @context)
56
- else
57
- ElemMatchMatcher.build({ '$eq' => @condition }, context: @context)
58
- end
59
- sort_and_adopt(result)
60
- end
61
-
62
- ELEM_MATCH_OPERATORS = %w($gt $gte $lt $lte $regex).freeze
63
-
64
- private
65
-
66
- # Parses the original condition hash into a normalized structure suitable for HashConditionMatcher.
67
- #
68
- # This method classifies keys in the condition hash as:
69
- # - Numeric (integers or numeric strings): treated as index-based field matchers
70
- # - Element-level comparison operators (`$gt`, `$gte`, `$lt`, `$lte`, `$regex`, non-array `$eq`):
71
- # grouped under `$elemMatch`
72
- # - Container-level operator keys (e.g., `$size`, `$in`, `$nin`, `$every`, `$ne`): retained at the top level
73
- # - All other keys: grouped under a `$elemMatch` clause for element-wise comparison
74
- #
75
- # @return [Hash] A normalized condition hash, potentially containing `$elemMatch`
76
- def parsed_condition
77
- h_parsed = {}
78
- h_elem_match = {}
79
- @condition.each_pair do |key, value|
80
- parse_condition_pair(key, value, h_parsed, h_elem_match)
81
- end
82
-
83
- h_parsed['$elemMatch'] = h_elem_match if is_present?(h_elem_match)
84
- h_parsed
85
- end
86
-
87
- def parse_condition_pair(key, value, h_parsed, h_elem_match)
88
- case key
89
- when Integer, FieldMatcher::INTEGER_INDEX_REGEX
90
- h_parsed[key.to_i] = value
91
- when '$elemMatch'
92
- value.is_a?(Hash) ? h_elem_match.merge!(value) : h_parsed[key] = value
93
- when *Matchers.operators
94
- elem_match_operator?(key, value) ? h_elem_match[key] = value : h_parsed[key] = value
95
- else
96
- h_elem_match[key] = value
97
- end
98
- end
99
-
100
- def elem_match_operator?(key, value)
101
- ELEM_MATCH_OPERATORS.include?(key) || (key == '$eq' && !value.is_a?(Array))
102
- end
103
- end
104
- end
105
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # ElemMatchMatcher implements the logic for Mongo-style `$elemMatch`.
6
- #
7
- # It is used to determine if *any* element in an array matches the given condition.
8
- #
9
- # This matcher delegates element-wise comparison to HashConditionMatcher,
10
- # allowing nested conditions to be applied recursively.
11
- #
12
- # Typically used internally by ArrayRecordMatcher when dealing with
13
- # non-indexed hash-style subconditions.
14
- #
15
- # @example
16
- # matcher = ElemMatchMatcher.build({ status: 'active' })
17
- # matcher.match?([{ status: 'inactive' }, { status: 'active' }]) #=> true
18
- #
19
- # @see HashConditionMatcher
20
- class ElemMatchMatcher < HashConditionMatcher
21
- # Creates a raw Proc that performs the element matching operation.
22
- # The Proc checks if any element in the array matches the condition.
23
- #
24
- # @return [Proc] a Proc that performs the element matching operation
25
- def raw_proc
26
- super_proc = super
27
- need_convert = @context.need_convert
28
- data_converter = Mongory.data_converter
29
-
30
- Proc.new do |collection|
31
- next false unless collection.is_a?(Array)
32
-
33
- collection.any? do |record|
34
- record = data_converter.convert(record) if need_convert
35
- super_proc.call(record)
36
- end
37
- end
38
- end
39
-
40
- def priority
41
- 3 + super
42
- end
43
-
44
- # Ensures the condition is a Hash.
45
- #
46
- # @raise [Mongory::TypeError] if the condition is not a Hash
47
- # @return [void]
48
- def check_validity!
49
- raise TypeError, '$elemMatch needs a Hash.' unless @condition.is_a?(Hash)
50
-
51
- super
52
- end
53
- end
54
-
55
- register(:elem_match, '$elemMatch', ElemMatchMatcher)
56
- end
57
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # EqMatcher matches values using the equality operator `==`.
6
- #
7
- # If the target record is an `Array`, it matches when either the array itself
8
- # equals the condition or any element in the array equals the condition.
9
- #
10
- # Used for conditions like:
11
- # - { age: { '$eq' => 30 } }
12
- # - { name: "Alice" } (implicit fallback)
13
- #
14
- # This matcher supports any Ruby object that implements `#==`.
15
- #
16
- # @example
17
- # matcher = EqMatcher.build(42)
18
- # matcher.match?(42) #=> true
19
- # matcher.match?([10, 42]) #=> true
20
- # matcher.match?("42") #=> false
21
- #
22
- # @note This matcher is also used as the fallback for non-operator literal values,
23
- # such as `{ name: "Alice" }`, when no other specialized matcher is applicable.
24
- #
25
- # @note Equality behavior depends on how `==` is implemented for the given objects.
26
- #
27
- # @see AbstractMatcher
28
- class EqMatcher < AbstractMatcher
29
- # Creates a raw Proc that performs the equality check.
30
- # The Proc uses the `==` operator to compare values.
31
- #
32
- # @return [Proc] a Proc that performs the equality check
33
- def raw_proc
34
- condition = @condition
35
-
36
- Proc.new do |record|
37
- record == condition || (record.is_a?(Array) && record.include?(condition))
38
- end
39
- end
40
-
41
- def priority
42
- 1
43
- end
44
- end
45
-
46
- register(:eq, '$eq', EqMatcher)
47
- end
48
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # EveryMatcher implements the logic for Mongo-style `$every` which is not really support in MongoDB.
6
- #
7
- # It is used to determine if *all* element in an array matches the given condition.
8
- #
9
- # This matcher delegates element-wise comparison to HashConditionMatcher,
10
- # allowing nested conditions to be applied recursively.
11
- #
12
- # @example
13
- # matcher = EveryMatcher.build({ status: 'active' })
14
- # matcher.match?([{ status: 'inactive' }, { status: 'active' }]) #=> false
15
- #
16
- # @see HashConditionMatcher
17
- class EveryMatcher < HashConditionMatcher
18
- # Creates a raw Proc that performs the element matching operation.
19
- # The Proc checks if all elements in the array match the condition.
20
- #
21
- # @return [Proc] A proc that performs element matching with context awareness
22
- # @note The proc includes error handling and context-based record conversion
23
- def raw_proc
24
- super_proc = super
25
- need_convert = @context.need_convert
26
- data_converter = Mongory.data_converter
27
-
28
- Proc.new do |collection|
29
- next false unless collection.is_a?(Array)
30
- next false if collection.empty?
31
-
32
- collection.all? do |record|
33
- record = data_converter.convert(record) if need_convert
34
- super_proc.call(record)
35
- end
36
- end
37
- end
38
-
39
- def priority
40
- 3 + super
41
- end
42
-
43
- # Ensures the condition is a Hash.
44
- #
45
- # @raise [Mongory::TypeError] if the condition is not a Hash
46
- # @return [void]
47
- def check_validity!
48
- raise TypeError, '$every needs a Hash.' unless @condition.is_a?(Hash)
49
-
50
- super
51
- end
52
- end
53
-
54
- register(:every, '$every', EveryMatcher)
55
- end
56
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # ExistsMatcher implements the `$exists` operator, which checks whether a key exists.
6
- #
7
- # It transforms the presence (or absence) of a field into a boolean value,
8
- # then compares it to the condition using the `==` operator.
9
- #
10
- # This matcher ensures the condition is strictly a boolean (`true` or `false`).
11
- #
12
- # @example
13
- # matcher = ExistsMatcher.build(true)
14
- # matcher.match?(42) #=> true
15
- # matcher.match?(KEY_NOT_FOUND) #=> false
16
- #
17
- # matcher = ExistsMatcher.build(false)
18
- # matcher.match?(KEY_NOT_FOUND) #=> true
19
- #
20
- # @see AbstractMatcher
21
- class ExistsMatcher < AbstractMatcher
22
- # Creates a raw Proc that performs the existence check.
23
- # The Proc checks if the record exists and compares it to the condition.
24
- #
25
- # @return [Proc] A proc that performs existence check with error handling
26
- def raw_proc
27
- condition = @condition
28
-
29
- Proc.new do |record|
30
- # Check if the record is nil or KEY_NOT_FOUND
31
- # and compare it to the condition.
32
- (record != KEY_NOT_FOUND) == condition
33
- end
34
- end
35
-
36
- def priority
37
- 2
38
- end
39
-
40
- # Ensures that the condition value is a valid boolean.
41
- #
42
- # @raise [TypeError] if condition is not true or false
43
- # @return [void]
44
- def check_validity!
45
- return if [true, false].include?(@condition)
46
-
47
- raise TypeError, "$exists needs a boolean, but got #{@condition.inspect}"
48
- end
49
- end
50
-
51
- register(:exists, '$exists', ExistsMatcher)
52
- end
53
- end