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
- def initialize(key, sort_type = {})
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
- @top_index_key = "#{key}::stack::top"
12
- initialize_document(@top_index_key, 0)
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 push(value)
17
- new_top_index = increase_atomic_count(@top_index_key)
18
- create_document("#{key}::stack::#{new_top_index}", value)
19
- self
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 pop()
23
- old_top_index = get_document(@top_index_key)
24
- decrease_atomic_count(@top_index_key)
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(@top_index_key)
50
+ doc = get_document(@list_key)
51
+ doc["sorted_list"].size
33
52
  end
34
53
  end
35
54
  end
@@ -1,3 +1,3 @@
1
1
  module CouchbaseStructures
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-structures
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: