ohm-sorted 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.md +8 -0
- data/lib/ohm/sorted.rb +29 -42
- data/ohm-sorted.gemspec +1 -1
- data/test/sorted_test.rb +9 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTQwNzhmZDJmMjY0ZDBiNDg1MzdlZDExZTI1ZjE2NjFhMGY3ZDc0NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2YyOGE2NzQwMmM4ZjZjYjAzYjA4NzU4MzlkMjBiNWI1ODhkYTg3Yg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzY1NTEzOTM3NGM4N2I2MjA1ZTBhNjFlMjZhNTRiYmYxNjNhNmYwMjkwMGVm
|
10
|
+
Y2Q2NjE3NTUxZDg1Y2YwNWExOGE1ODc3MjBiZjU0NWViZmU5YjUxZDA3MDYy
|
11
|
+
NGU3OWQwODc0ZjMxOGJiMzU2ODJhZjJmOTkzMzczYzFmYzNhNGY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjVjMTljNzBlMjMzNGQyYzI0ODBhNWIyMTQ5NDU0ZmQ3ZjgxNDM1M2FjYzBi
|
14
|
+
M2I2NTQ1ZDE5YzE0YTUxYWU4YjI4NTVjYWU4NzczMzk5YmFjMDdlMjdjMzU2
|
15
|
+
OGQ4MDFjNGE0OWM3NTk2MzA1ODA0ZjZmYjFjNWQwZjgzNTEyODg=
|
data/CHANGELOG.md
CHANGED
data/lib/ohm/sorted.rb
CHANGED
@@ -15,10 +15,6 @@ module Ohm
|
|
15
15
|
@options = options
|
16
16
|
end
|
17
17
|
|
18
|
-
def range
|
19
|
-
@options.fetch(:range, "-inf".."inf")
|
20
|
-
end
|
21
|
-
|
22
18
|
def offset
|
23
19
|
@options.fetch(:offset, 0)
|
24
20
|
end
|
@@ -27,10 +23,14 @@ module Ohm
|
|
27
23
|
@options.fetch(:count, -1)
|
28
24
|
end
|
29
25
|
|
26
|
+
def size
|
27
|
+
execute { |key| db.zcard(key) }
|
28
|
+
end
|
29
|
+
|
30
30
|
def between(first, last)
|
31
31
|
range = first.to_f..last.to_f
|
32
32
|
opts = @options.merge(range: range)
|
33
|
-
|
33
|
+
RangedSortedSet.new(key, namespace, model, opts)
|
34
34
|
end
|
35
35
|
|
36
36
|
def slice(*args)
|
@@ -39,17 +39,22 @@ module Ohm
|
|
39
39
|
elsif args.count == 2
|
40
40
|
offset, count = *args
|
41
41
|
opts = @options.merge(offset: offset, count: count)
|
42
|
-
|
42
|
+
self.class.new(key, namespace, model, opts)
|
43
43
|
else
|
44
44
|
raise ArgumentError
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
def first
|
49
|
+
slice(0, 1).to_a.first
|
50
|
+
end
|
51
|
+
|
48
52
|
def ids
|
49
|
-
execute
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
+
execute { |key| db.zrangebyscore(key, "-inf", "inf", limit: [offset, count]) }
|
54
|
+
end
|
55
|
+
|
56
|
+
def inspect
|
57
|
+
"#<SortedSet (#{model}): #{ids}>"
|
53
58
|
end
|
54
59
|
end
|
55
60
|
|
@@ -57,14 +62,6 @@ module Ohm
|
|
57
62
|
class SortedSet < BasicSet
|
58
63
|
include SortedMethods
|
59
64
|
|
60
|
-
def size
|
61
|
-
execute { |key| db.zcard(key) }
|
62
|
-
end
|
63
|
-
|
64
|
-
def first
|
65
|
-
fetch(execute { |key| db.zrange(key, 0, 1) }).first
|
66
|
-
end
|
67
|
-
|
68
65
|
private
|
69
66
|
def exists?(id)
|
70
67
|
execute { |key| !!db.zscore(key, id) }
|
@@ -82,17 +79,6 @@ module Ohm
|
|
82
79
|
class SortedSet < Model::Collection
|
83
80
|
include Ohm::SortedMethods
|
84
81
|
|
85
|
-
attr :key
|
86
|
-
attr :namespace
|
87
|
-
attr :model
|
88
|
-
|
89
|
-
def initialize(key, namespace, model, options={})
|
90
|
-
@key = key
|
91
|
-
@model = model
|
92
|
-
@namespace = namespace
|
93
|
-
@options = options
|
94
|
-
end
|
95
|
-
|
96
82
|
def db
|
97
83
|
model.db
|
98
84
|
end
|
@@ -105,10 +91,6 @@ module Ohm
|
|
105
91
|
model[id] if !!db.zrank(key, id)
|
106
92
|
end
|
107
93
|
|
108
|
-
def size
|
109
|
-
db.zcard(key)
|
110
|
-
end
|
111
|
-
|
112
94
|
def empty?
|
113
95
|
size == 0
|
114
96
|
end
|
@@ -117,19 +99,10 @@ module Ohm
|
|
117
99
|
ids.map(&model)
|
118
100
|
end
|
119
101
|
|
120
|
-
def first
|
121
|
-
id = db.zrange(key, 0, 1).first
|
122
|
-
model[id] unless id.empty?
|
123
|
-
end
|
124
|
-
|
125
102
|
def include?(model)
|
126
103
|
!!db.zrank(key, model.id)
|
127
104
|
end
|
128
105
|
|
129
|
-
def inspect
|
130
|
-
"#<SortedSet (#{model}): #{db.zrange(key, 0, -1).inspect}>"
|
131
|
-
end
|
132
|
-
|
133
106
|
private
|
134
107
|
def execute
|
135
108
|
yield key
|
@@ -137,6 +110,20 @@ module Ohm
|
|
137
110
|
end
|
138
111
|
end
|
139
112
|
|
113
|
+
class RangedSortedSet < SortedSet
|
114
|
+
def range
|
115
|
+
@options.fetch(:range)
|
116
|
+
end
|
117
|
+
|
118
|
+
def ids
|
119
|
+
execute { |key| db.zrangebyscore(key, range.begin, range.end, limit: [offset, count]) }
|
120
|
+
end
|
121
|
+
|
122
|
+
def size
|
123
|
+
execute { |key| db.zcount(key, range.begin, range.end) }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
140
127
|
module Sorted
|
141
128
|
def self.included(model)
|
142
129
|
model.extend(ClassMethods)
|
data/ohm-sorted.gemspec
CHANGED
data/test/sorted_test.rb
CHANGED
@@ -140,6 +140,15 @@ class SortedTest < Test::Unit::TestCase
|
|
140
140
|
assert_equal 1, sorted_set.size
|
141
141
|
end
|
142
142
|
|
143
|
+
def test_sorted_set_between_size
|
144
|
+
4.times do |i|
|
145
|
+
Post.create(order: i)
|
146
|
+
end
|
147
|
+
|
148
|
+
sorted_set = Post.sorted_find(:order).between(2, 3)
|
149
|
+
assert_equal 2, sorted_set.size
|
150
|
+
end
|
151
|
+
|
143
152
|
def test_sorted_set_empty
|
144
153
|
sorted_set = Post.sorted_find(:order)
|
145
154
|
assert sorted_set.empty?
|