mongory 0.9.0 → 2.0.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.
Files changed (85) 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/generators/mongory/install/templates/initializer.rb.erb +0 -5
  45. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  46. data/lib/mongory/converters/converted.rb +28 -6
  47. data/lib/mongory/converters/key_converter.rb +2 -0
  48. data/lib/mongory/matcher.rb +63 -0
  49. data/lib/mongory/matchers/abstract_matcher.rb +18 -164
  50. data/lib/mongory/matchers.rb +68 -84
  51. data/lib/mongory/query_builder.rb +30 -41
  52. data/lib/mongory/utils/tree_renderer.rb +108 -0
  53. data/lib/mongory/utils.rb +2 -67
  54. data/lib/mongory/version.rb +1 -1
  55. data/lib/mongory.rb +9 -59
  56. data/script/bench.rb +54 -0
  57. metadata +13 -37
  58. data/lib/mongory/c_matcher.rb +0 -57
  59. data/lib/mongory/c_query_builder.rb +0 -44
  60. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -124
  61. data/lib/mongory/matchers/and_matcher.rb +0 -72
  62. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  63. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  64. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  65. data/lib/mongory/matchers/every_matcher.rb +0 -56
  66. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  67. data/lib/mongory/matchers/field_matcher.rb +0 -160
  68. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  69. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  70. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -79
  71. data/lib/mongory/matchers/in_matcher.rb +0 -84
  72. data/lib/mongory/matchers/literal_matcher.rb +0 -121
  73. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  74. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  75. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  76. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  77. data/lib/mongory/matchers/not_matcher.rb +0 -40
  78. data/lib/mongory/matchers/or_matcher.rb +0 -68
  79. data/lib/mongory/matchers/present_matcher.rb +0 -55
  80. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  81. data/lib/mongory/matchers/size_matcher.rb +0 -54
  82. data/lib/mongory/query_matcher.rb +0 -93
  83. data/lib/mongory/rails.rb +0 -15
  84. data/lib/mongory/utils/debugger.rb +0 -125
  85. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -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
- result.sort_by(&:priority)
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
@@ -1,160 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # FieldMatcher handles field-level matching by extracting and comparing field values.
6
- #
7
- # This matcher is responsible for:
8
- # 1. Extracting field values from records using dot notation
9
- # 2. Converting extracted values if needed
10
- # 3. Delegating the actual comparison to a submatcher
11
- #
12
- # It supports:
13
- # - Hash records with string/symbol keys
14
- # - Array records with numeric indices
15
- # - Objects that respond to `[]`
16
- #
17
- # @example Basic field matching
18
- # matcher = FieldMatcher.build('age', 30)
19
- # matcher.match?({ 'age' => 30 }) #=> true
20
- # matcher.match?({ age: 30 }) #=> true
21
- #
22
- # @see LiteralMatcher
23
- class FieldMatcher < LiteralMatcher
24
- # A list of classes that should never be used for value digging.
25
- # These typically respond to `#[]` but are semantically invalid for this context.
26
- CLASSES_NOT_ALLOW_TO_DIG = [
27
- ::String,
28
- ::Integer,
29
- ::Proc,
30
- ::Method,
31
- ::MatchData,
32
- ::Thread,
33
- ::Symbol
34
- ].freeze
35
-
36
- INTEGER_INDEX_REGEX = /\A(?:0|-?[1-9]\d*)\z/.freeze
37
-
38
- # Initializes a new field matcher.
39
- #
40
- # @param field [String, Symbol] the field to match against
41
- # @param condition [Object] the condition to match with
42
- # @param context [Context] the query context
43
- def initialize(field, condition, context: Context.new)
44
- @field = field
45
- super(condition, context: context)
46
- end
47
-
48
- # Creates a raw Proc that performs the field matching operation.
49
- # The Proc extracts the field value and delegates to the submatcher.
50
- #
51
- # This method first ensures the record is structurally eligible for field extraction—
52
- # it must be a Hash, Array, or respond to `[]`. If the structure does not allow for
53
- # field access (e.g., nil, primitive values, or unsupported types), the match returns false.
54
- #
55
- # The field value is then extracted using the following rules:
56
- # - If the record is a Hash, it attempts to fetch using the field key,
57
- # falling back to symbolized key if needed.
58
- # - If the record is an Array, it fetches by index.
59
- # - If the record does not support `[]` or is disallowed for dig operations,
60
- # the match returns false immediately.
61
- #
62
- # Once the value is extracted, it is passed through the data converter
63
- # and matched against the condition via the superclass.
64
- #
65
- # @example Matching a Hash with a nil field value
66
- # matcher = Mongory::QueryMatcher.new(a: nil)
67
- # matcher.match?({ a: nil }) # => true
68
- #
69
- # @example Record is nil (structure not diggable)
70
- # matcher = Mongory::QueryMatcher.new(a: nil)
71
- # matcher.match?(nil) # => false
72
- #
73
- # @example Matching against an Array by index
74
- # matcher = Mongory::QueryMatcher.new(0 => /abc/)
75
- # matcher.match?(['abcdef']) # => true
76
- #
77
- # @example Hash with symbol key, matcher uses string key
78
- # matcher = Mongory::QueryMatcher.new('a' => 123)
79
- # matcher.match?({ a: 123 }) # => true
80
-
81
- # Creates a raw Proc that performs the field-based matching operation.
82
- # The Proc extracts the field value and delegates matching to the superclass.
83
- #
84
- # @return [Proc] A proc that performs field-based matching with context awareness
85
- # @note The proc handles field extraction and delegates matching to the superclass
86
- def raw_proc
87
- super_proc = super
88
- field = @field
89
- str_field, sym_field, int_field = parse_field_keys(field)
90
- need_convert = @context.need_convert
91
- data_converter = Mongory.data_converter
92
-
93
- Proc.new do |record|
94
- sub_record =
95
- case record
96
- when Hash
97
- record.fetch(str_field) do
98
- record.fetch(sym_field, KEY_NOT_FOUND)
99
- end
100
- when Array
101
- int_field ? record.fetch(int_field, KEY_NOT_FOUND) : KEY_NOT_FOUND
102
- when KEY_NOT_FOUND, *CLASSES_NOT_ALLOW_TO_DIG
103
- next false
104
- else
105
- next false unless record.respond_to?(:[])
106
-
107
- record[field]
108
- end
109
-
110
- sub_record = data_converter.convert(sub_record) if need_convert
111
- super_proc.call(sub_record)
112
- end
113
- end
114
-
115
- def priority
116
- 1 + super
117
- end
118
-
119
- # Returns a unique key for this matcher, including the field name.
120
- # Used for deduplication in multi-matchers.
121
- #
122
- # @return [String] a unique key for this matcher
123
- # @see AbstractMultiMatcher#matchers
124
- def uniq_key
125
- super + "field:#{@field}"
126
- end
127
-
128
- private
129
-
130
- def parse_field_keys(field)
131
- str_field = field.to_s
132
- int_field = if field.is_a?(Integer)
133
- field
134
- elsif field.is_a?(String) && INTEGER_INDEX_REGEX.match?(field)
135
- field.to_i
136
- end
137
- [str_field, str_field.to_sym, int_field]
138
- end
139
-
140
- # Returns a single-line summary of the field matcher including the field and condition.
141
- #
142
- # @return [String] a formatted title for tree display
143
- def tree_title
144
- "Field: #{@field.inspect} to match: #{@condition.inspect}"
145
- end
146
-
147
- # Custom display logic for debugging, including colored field highlighting.
148
- #
149
- # @param record [Object] the input record
150
- # @param result [Boolean] match result
151
- # @return [String] formatted debug string with highlighted field
152
- def debug_display(record, result)
153
- "#{self.class.name.split('::').last} #{colored_result(result)}, " \
154
- "condition: #{@condition.inspect}, " \
155
- "\e[30;47mfield: #{@field.inspect}\e[0m, " \
156
- "record: #{record.inspect.gsub(@field.inspect, "\e[30;47m#{@field.inspect}\e[0m")}"
157
- end
158
- end
159
- end
160
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # GtMatcher implements the `$gt` (greater than) operator.
6
- #
7
- # It returns true if the record is strictly greater than the condition.
8
- #
9
- # Inherits core logic from AbstractMatcher, including
10
- # error handling and optional preprocessing.
11
- #
12
- # @example
13
- # matcher = GtMatcher.build(10)
14
- # matcher.match?(15) #=> true
15
- # matcher.match?(10) #=> false
16
- #
17
- # @see AbstractMatcher
18
- class GtMatcher < AbstractMatcher
19
- # Creates a raw Proc that performs the greater-than comparison.
20
- # The Proc uses the `>` operator to compare values.
21
- #
22
- # @return [Proc] A proc that performs greater-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(:gt, '$gt', GtMatcher)
40
- end
41
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # GteMatcher implements the `$gte` (greater than or equal) operator.
6
- #
7
- # It returns true if the record is greater than or equal to the condition value.
8
- #
9
- # Inherits comparison logic and error safety from AbstractMatcher.
10
- #
11
- # @example
12
- # matcher = GteMatcher.build(10)
13
- # matcher.match?(10) #=> true
14
- # matcher.match?(11) #=> true
15
- # matcher.match?(9) #=> false
16
- #
17
- # @see AbstractMatcher
18
- class GteMatcher < AbstractMatcher
19
- # Creates a raw Proc that performs the greater-than-or-equal comparison.
20
- # The Proc uses the `>=` operator to compare values.
21
- #
22
- # @return [Proc] A proc that performs greater-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(:gte, '$gte', GteMatcher)
40
- end
41
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongory
4
- module Matchers
5
- # HashConditionMatcher is responsible for handling field-level query conditions.
6
- #
7
- # It receives a Hash of key-value pairs and delegates each one to an appropriate matcher
8
- # based on whether the key is a recognized operator or a data field path.
9
- #
10
- # Each subcondition is matched independently using the `:all?` strategy, meaning
11
- # all subconditions must match for the entire HashConditionMatcher to succeed.
12
- # For empty conditions, it returns true (using TRUE_PROC).
13
- #
14
- # This matcher plays a central role in dispatching symbolic query conditions
15
- # to the appropriate field or operator matcher.
16
- #
17
- # @example Basic field matching
18
- # matcher = HashConditionMatcher.build({ age: { :$gt => 30 }, active: true })
19
- # matcher.match?(record) #=> true only if all subconditions match
20
- #
21
- # @example Empty conditions
22
- # matcher = HashConditionMatcher.build({})
23
- # matcher.match?(record) #=> true (uses TRUE_PROC)
24
- #
25
- # @see AbstractMultiMatcher
26
- class HashConditionMatcher < AbstractMultiMatcher
27
- enable_unwrap!
28
-
29
- # Creates a raw Proc that performs the hash condition matching operation.
30
- # The Proc combines all submatcher Procs and returns true only if all match.
31
- # For empty conditions, returns TRUE_PROC.
32
- #
33
- # @return [Proc] a Proc that performs the hash condition matching operation
34
- def raw_proc
35
- combine_procs_with_and(*matchers.map(&:to_proc))
36
- end
37
-
38
- # Returns the list of matchers for each key-value pair in the condition.
39
- #
40
- # For each pair:
41
- # - `$regex` with a sibling `$options` builds a RegexMatcher with those flags
42
- # - If the key is a registered operator, uses the corresponding matcher
43
- # - An unknown `$` key raises, as MongoDB rejects unknown operators
44
- # - Otherwise, wraps the value in a FieldMatcher for field path matching
45
- #
46
- # @return [Array<AbstractMatcher>] List of matchers for each condition
47
- # @raise [TypeError] for an unknown operator or a `$options` without `$regex`
48
- define_instance_cache_method(:matchers) do
49
- @condition.map do |key, value|
50
- next if key == '$options' && @condition.key?('$regex')
51
-
52
- build_matcher(key, value)
53
- end.compact.sort_by(&:priority)
54
- end
55
-
56
- def check_validity!
57
- return super if @condition.is_a?(Hash)
58
-
59
- raise TypeError, 'condition needs a Hash.'
60
- end
61
-
62
- private
63
-
64
- def build_matcher(key, value)
65
- if key == '$regex' && @condition.key?('$options')
66
- RegexMatcher.build(RegexMatcher.apply_options(value, @condition['$options']), context: @context)
67
- elsif (matcher_class = Matchers.lookup(key))
68
- matcher_class.build(value, context: @context)
69
- elsif key == '$options'
70
- raise TypeError, '$options needs a sibling $regex'
71
- elsif key.to_s.start_with?('$')
72
- raise TypeError, "unknown operator #{key}"
73
- else
74
- FieldMatcher.build(key, value, context: @context)
75
- end
76
- end
77
- end
78
- end
79
- end