@@ -135,7 +135,7 @@ def config(action, *args)
135
135
synchronize do |client |
136
136
client . call ( [ :config , action ] + args ) do |reply |
137
137
if reply . kind_of? ( Array ) && action == :get
138
- Hash [ reply . each_slice ( 2 ) . to_a ]
138
+ Hash [ _pairify ( reply ) ]
139
139
else
140
140
reply
141
141
end
@@ -639,9 +639,7 @@ def incrby(key, increment)
639
639
# @return [Float] value after incrementing it
640
640
def incrbyfloat ( key , increment )
641
641
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 )
645
643
end
646
644
end
647
645
@@ -1417,9 +1415,7 @@ def zadd(key, *args)
1417
1415
# @return [Float] score of the member after incrementing it
1418
1416
def zincrby ( key , increment , member )
1419
1417
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 )
1423
1419
end
1424
1420
end
1425
1421
@@ -1465,9 +1461,7 @@ def zrem(key, member)
1465
1461
# @return [Float] score of the member
1466
1462
def zscore ( key , member )
1467
1463
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 )
1471
1465
end
1472
1466
end
1473
1467
@@ -1493,20 +1487,14 @@ def zrange(key, start, stop, options = {})
1493
1487
args = [ ]
1494
1488
1495
1489
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
1497
1495
1498
1496
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 )
1510
1498
end
1511
1499
end
1512
1500
@@ -1525,20 +1513,14 @@ def zrevrange(key, start, stop, options = {})
1525
1513
args = [ ]
1526
1514
1527
1515
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
1529
1521
1530
1522
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 )
1542
1524
end
1543
1525
end
1544
1526
@@ -1615,23 +1597,17 @@ def zrangebyscore(key, min, max, options = {})
1615
1597
args = [ ]
1616
1598
1617
1599
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
1619
1605
1620
1606
limit = options [ :limit ]
1621
1607
args . concat ( [ "LIMIT" ] + limit ) if limit
1622
1608
1623
1609
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 )
1635
1611
end
1636
1612
end
1637
1613
@@ -1653,23 +1629,17 @@ def zrevrangebyscore(key, max, min, options = {})
1653
1629
args = [ ]
1654
1630
1655
1631
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
1657
1637
1658
1638
limit = options [ :limit ]
1659
1639
args . concat ( [ "LIMIT" ] + limit ) if limit
1660
1640
1661
1641
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 )
1673
1643
end
1674
1644
end
1675
1645
@@ -1931,9 +1901,7 @@ def hincrby(key, field, increment)
1931
1901
# @return [Float] value of the field after incrementing it
1932
1902
def hincrbyfloat ( key , field , increment )
1933
1903
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 )
1937
1905
end
1938
1906
end
1939
1907
@@ -2325,7 +2293,7 @@ def scan(cursor, options={})
2325
2293
# @return [String, Array<[String, String]>] the next cursor and all found keys
2326
2294
def hscan ( key , cursor , options = { } )
2327
2295
_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 ] ) ]
2329
2297
end
2330
2298
end
2331
2299
@@ -2343,7 +2311,7 @@ def hscan(key, cursor, options={})
2343
2311
# members and scores
2344
2312
def zscan ( key , cursor , options = { } )
2345
2313
_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 ] ) ]
2347
2315
end
2348
2316
end
2349
2317
@@ -2411,12 +2379,30 @@ def _hashify
2411
2379
}
2412
2380
end
2413
2381
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
2420
2406
end
2421
2407
2422
2408
def _subscription ( method , channels , block )
0 commit comments