couchbase-structures 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -5,31 +5,50 @@ module CouchbaseStructures
|
|
5
5
|
class SortedList
|
6
6
|
include CouchbaseDocStore
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
attr_accessor :sort_type, :sort_key
|
9
|
+
|
10
|
+
def initialize(key, attr = {})
|
11
|
+
|
12
|
+
if attr.has_key? :sort_type
|
13
|
+
case attr[:sort_type]
|
14
|
+
when :json
|
15
|
+
@sort_type = :json
|
16
|
+
@sort_key = attr[:sort_key]
|
17
|
+
when :simple
|
18
|
+
@sort_type = :simple
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
10
22
|
@key = key
|
11
|
-
@
|
12
|
-
initialize_document(@
|
13
|
-
self
|
23
|
+
@list_key = "#{key}::sorted_list"
|
24
|
+
initialize_document(@list_key, { :sorted_list => [], :last_updated => Time.now.utc.to_i })
|
25
|
+
self
|
14
26
|
end
|
15
27
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
28
|
+
def add(value)
|
29
|
+
doc = get_document(@list_key)
|
30
|
+
list = doc["sorted_list"]
|
31
|
+
list << value
|
32
|
+
|
33
|
+
# simple sort only for singular values, string, integer, float, etc.
|
34
|
+
list.sort!
|
35
|
+
|
36
|
+
# complex sort for Hash types
|
37
|
+
|
38
|
+
doc["sorted_list"] = list
|
39
|
+
doc["last_updated"] = Time.now.utc.to_i
|
40
|
+
replace_document(@list_key, doc)
|
41
|
+
list
|
20
42
|
end
|
21
43
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
doc = get_document("#{key}::stack::#{old_top_index}")
|
27
|
-
delete_document("#{key}::stack::#{old_top_index}")
|
28
|
-
doc
|
44
|
+
def items
|
45
|
+
doc = get_document(@list_key)
|
46
|
+
doc["sorted_list"]
|
29
47
|
end
|
30
48
|
|
31
49
|
def size
|
32
|
-
get_document(@
|
50
|
+
doc = get_document(@list_key)
|
51
|
+
doc["sorted_list"].size
|
33
52
|
end
|
34
53
|
end
|
35
54
|
end
|