Skip to content

Commit 0c362af

Browse files
committed
DRY up reply transformation.
1 parent 998d6f0 commit 0c362af

File tree

1 file changed

+55
-69
lines changed

1 file changed

+55
-69
lines changed

lib/redis.rb

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def config(action, *args)
135135
synchronize do |client|
136136
client.call([:config, action] + args) do |reply|
137137
if reply.kind_of?(Array) && action == :get
138-
Hash[reply.each_slice(2).to_a]
138+
Hash[_pairify(reply)]
139139
else
140140
reply
141141
end
@@ -639,9 +639,7 @@ def incrby(key, increment)
639639
# @return [Float] value after incrementing it
640640
def incrbyfloat(key, increment)
641641
synchronize do |client|
642-
client.call([:incrbyfloat, key, increment]) do |reply|
643-
_floatify(reply) if reply
644-
end
642+
client.call([:incrbyfloat, key, increment], &_floatify)
645643
end
646644
end
647645

@@ -1417,9 +1415,7 @@ def zadd(key, *args)
14171415
# @return [Float] score of the member after incrementing it
14181416
def zincrby(key, increment, member)
14191417
synchronize do |client|
1420-
client.call([:zincrby, key, increment, member]) do |reply|
1421-
_floatify(reply) if reply
1422-
end
1418+
client.call([:zincrby, key, increment, member], &_floatify)
14231419
end
14241420
end
14251421

@@ -1465,9 +1461,7 @@ def zrem(key, member)
14651461
# @return [Float] score of the member
14661462
def zscore(key, member)
14671463
synchronize do |client|
1468-
client.call([:zscore, key, member]) do |reply|
1469-
_floatify(reply) if reply
1470-
end
1464+
client.call([:zscore, key, member], &_floatify)
14711465
end
14721466
end
14731467

@@ -1493,20 +1487,14 @@ def zrange(key, start, stop, options = {})
14931487
args = []
14941488

14951489
with_scores = options[:with_scores] || options[:withscores]
1496-
args << "WITHSCORES" if with_scores
1490+
1491+
if with_scores
1492+
args << "WITHSCORES"
1493+
block = _floatify_pairs
1494+
end
14971495

14981496
synchronize do |client|
1499-
client.call([:zrange, key, start, stop] + args) do |reply|
1500-
if with_scores
1501-
if reply
1502-
reply.each_slice(2).map do |member, score|
1503-
[member, _floatify(score)]
1504-
end
1505-
end
1506-
else
1507-
reply
1508-
end
1509-
end
1497+
client.call([:zrange, key, start, stop] + args, &block)
15101498
end
15111499
end
15121500

@@ -1525,20 +1513,14 @@ def zrevrange(key, start, stop, options = {})
15251513
args = []
15261514

15271515
with_scores = options[:with_scores] || options[:withscores]
1528-
args << "WITHSCORES" if with_scores
1516+
1517+
if with_scores
1518+
args << "WITHSCORES"
1519+
block = _floatify_pairs
1520+
end
15291521

15301522
synchronize do |client|
1531-
client.call([:zrevrange, key, start, stop] + args) do |reply|
1532-
if with_scores
1533-
if reply
1534-
reply.each_slice(2).map do |member, score|
1535-
[member, _floatify(score)]
1536-
end
1537-
end
1538-
else
1539-
reply
1540-
end
1541-
end
1523+
client.call([:zrevrange, key, start, stop] + args, &block)
15421524
end
15431525
end
15441526

@@ -1615,23 +1597,17 @@ def zrangebyscore(key, min, max, options = {})
16151597
args = []
16161598

16171599
with_scores = options[:with_scores] || options[:withscores]
1618-
args.concat(["WITHSCORES"]) if with_scores
1600+
1601+
if with_scores
1602+
args << "WITHSCORES"
1603+
block = _floatify_pairs
1604+
end
16191605

16201606
limit = options[:limit]
16211607
args.concat(["LIMIT"] + limit) if limit
16221608

16231609
synchronize do |client|
1624-
client.call([:zrangebyscore, key, min, max] + args) do |reply|
1625-
if with_scores
1626-
if reply
1627-
reply.each_slice(2).map do |member, score|
1628-
[member, _floatify(score)]
1629-
end
1630-
end
1631-
else
1632-
reply
1633-
end
1634-
end
1610+
client.call([:zrangebyscore, key, min, max] + args, &block)
16351611
end
16361612
end
16371613

@@ -1653,23 +1629,17 @@ def zrevrangebyscore(key, max, min, options = {})
16531629
args = []
16541630

16551631
with_scores = options[:with_scores] || options[:withscores]
1656-
args.concat(["WITHSCORES"]) if with_scores
1632+
1633+
if with_scores
1634+
args << ["WITHSCORES"]
1635+
block = _floatify_pairs
1636+
end
16571637

16581638
limit = options[:limit]
16591639
args.concat(["LIMIT"] + limit) if limit
16601640

16611641
synchronize do |client|
1662-
client.call([:zrevrangebyscore, key, max, min] + args) do |reply|
1663-
if with_scores
1664-
if reply
1665-
reply.each_slice(2).map do |member, score|
1666-
[member, _floatify(score)]
1667-
end
1668-
end
1669-
else
1670-
reply
1671-
end
1672-
end
1642+
client.call([:zrevrangebyscore, key, max, min] + args, &block)
16731643
end
16741644
end
16751645

@@ -1931,9 +1901,7 @@ def hincrby(key, field, increment)
19311901
# @return [Float] value of the field after incrementing it
19321902
def hincrbyfloat(key, field, increment)
19331903
synchronize do |client|
1934-
client.call([:hincrbyfloat, key, field, increment]) do |reply|
1935-
_floatify(reply) if reply
1936-
end
1904+
client.call([:hincrbyfloat, key, field, increment], &_floatify)
19371905
end
19381906
end
19391907

@@ -2325,7 +2293,7 @@ def scan(cursor, options={})
23252293
# @return [String, Array<[String, String]>] the next cursor and all found keys
23262294
def hscan(key, cursor, options={})
23272295
_scan(:hscan, cursor, options.merge(:key => key)) do |reply|
2328-
[reply[0], reply[1].each_slice(2).to_a]
2296+
[reply[0], _pairify(reply[1])]
23292297
end
23302298
end
23312299

@@ -2343,7 +2311,7 @@ def hscan(key, cursor, options={})
23432311
# members and scores
23442312
def zscan(key, cursor, options={})
23452313
_scan(:zscan, cursor, options.merge(:key => key)) do |reply|
2346-
[reply[0], reply[1].each_slice(2).map{|k,s| [k, _floatify(s)]}]
2314+
[reply[0], _floatify_pairs.call(reply[1])]
23472315
end
23482316
end
23492317

@@ -2411,12 +2379,30 @@ def _hashify
24112379
}
24122380
end
24132381

2414-
def _floatify(str)
2415-
if (inf = str.match(/^(-)?inf/i))
2416-
(inf[1] ? -1.0 : 1.0) / 0.0
2417-
else
2418-
Float str
2419-
end
2382+
def _floatify
2383+
lambda { |str|
2384+
return unless str
2385+
2386+
if (inf = str.match(/^(-)?inf/i))
2387+
(inf[1] ? -1.0 : 1.0) / 0.0
2388+
else
2389+
Float(str)
2390+
end
2391+
}
2392+
end
2393+
2394+
def _floatify_pairs
2395+
lambda { |array|
2396+
return unless array
2397+
2398+
array.each_slice(2).map do |member, score|
2399+
[member, _floatify.call(score)]
2400+
end
2401+
}
2402+
end
2403+
2404+
def _pairify(array)
2405+
array.each_slice(2).to_a
24202406
end
24212407

24222408
def _subscription(method, channels, block)

0 commit comments

Comments
 (0)