Skip to content

Commit fe0c506

Browse files
pimpinclaude
andcommitted
refactor: extract duplicated resolve lambda into resolve_value method
The lambda ->(v) { params ? bind(v, params) : quote(v) } was redefined in build_match, build_match_hash, build_match_range and build_not_match. Replace the four copies with a single resolve_value(value, params) helper. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 29e2dfc commit fe0c506

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

lib/couchbase-orm/utilities/query_helper.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ def bind(value, params)
2525
end
2626
end
2727

28+
# Renders a value either as a positional parameter (when +params+ is
29+
# provided) or as an inline quoted literal (when it is nil).
30+
def resolve_value(value, params)
31+
params ? bind(value, params) : quote(value)
32+
end
33+
2834
def build_match(key, value, params: nil)
2935
use_is_null = self.properties_always_exists_in_document
3036
key = "meta().id" if key.to_s == "id"
31-
resolve = ->(v) { params ? bind(v, params) : quote(v) }
3237
case
3338
when value.nil? && use_is_null
3439
"#{key} IS NULL"
@@ -41,29 +46,28 @@ def build_match(key, value, params: nil)
4146
when value.is_a?(Array) && value.include?(nil)
4247
"(#{build_match(key, nil, params: params)} OR #{build_match(key, value.compact, params: params)})"
4348
when value.is_a?(Array)
44-
"#{key} IN #{resolve.call(value)}"
49+
"#{key} IN #{resolve_value(value, params)}"
4550
when value.is_a?(Range)
4651
build_match_range(key, value, params: params)
4752
else
48-
"#{key} = #{resolve.call(value)}"
53+
"#{key} = #{resolve_value(value, params)}"
4954
end
5055
end
5156

5257
def build_match_hash(key, value, params: nil)
5358
matches = []
54-
resolve = ->(v) { params ? bind(v, params) : quote(v) }
5559
value.each do |k, v|
5660
case k
5761
when :_gt
58-
matches << "#{key} > #{resolve.call(v)}"
62+
matches << "#{key} > #{resolve_value(v, params)}"
5963
when :_gte
60-
matches << "#{key} >= #{resolve.call(v)}"
64+
matches << "#{key} >= #{resolve_value(v, params)}"
6165
when :_lt
62-
matches << "#{key} < #{resolve.call(v)}"
66+
matches << "#{key} < #{resolve_value(v, params)}"
6367
when :_lte
64-
matches << "#{key} <= #{resolve.call(v)}"
68+
matches << "#{key} <= #{resolve_value(v, params)}"
6569
when :_ne
66-
matches << "#{key} != #{resolve.call(v)}"
70+
matches << "#{key} != #{resolve_value(v, params)}"
6771

6872
# TODO v2
6973
# when :_in
@@ -111,13 +115,12 @@ def build_match_hash(key, value, params: nil)
111115
end
112116

113117
def build_match_range(key, value, params: nil)
114-
resolve = ->(v) { params ? bind(v, params) : quote(v) }
115118
matches = []
116-
matches << "#{key} >= #{resolve.call(value.begin)}"
119+
matches << "#{key} >= #{resolve_value(value.begin, params)}"
117120
if value.exclude_end?
118-
matches << "#{key} < #{resolve.call(value.end)}"
121+
matches << "#{key} < #{resolve_value(value.end, params)}"
119122
else
120-
matches << "#{key} <= #{resolve.call(value.end)}"
123+
matches << "#{key} <= #{resolve_value(value.end, params)}"
121124
end
122125
matches.join(" AND ")
123126
end
@@ -126,7 +129,6 @@ def build_match_range(key, value, params: nil)
126129
def build_not_match(key, value, params: nil)
127130
use_is_null = self.properties_always_exists_in_document
128131
key = "meta().id" if key.to_s == "id"
129-
resolve = ->(v) { params ? bind(v, params) : quote(v) }
130132
case
131133
when value.nil? && use_is_null
132134
"#{key} IS NOT NULL"
@@ -135,9 +137,9 @@ def build_not_match(key, value, params: nil)
135137
when value.is_a?(Array) && value.include?(nil)
136138
"(#{build_not_match(key, nil, params: params)} AND #{build_not_match(key, value.compact, params: params)})"
137139
when value.is_a?(Array)
138-
"#{key} NOT IN #{resolve.call(value)}"
140+
"#{key} NOT IN #{resolve_value(value, params)}"
139141
else
140-
"#{key} != #{resolve.call(value)}"
142+
"#{key} != #{resolve_value(value, params)}"
141143
end
142144
end
143145

0 commit comments

Comments
 (0)