Class: CouchbaseStructures::SortedList

Inherits:
Object
  • Object
show all
Includes:
CouchbaseDocStore
Defined in:
lib/couchbase_structures/sorted_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, attr = {}) ⇒ SortedList

Returns a new instance of SortedList.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/couchbase_structures/sorted_list.rb', line 10

def initialize(key, attr = {})
  
  if attr.has_key? :sort_type
    case attr[:sort_type]
    when :json
      @sort_type = :json
      @sort_key = attr[:sort_key] 
    when :simple
      @sort_type = :simple
    end
  end
  
  @key = key
  @list_key = "#{key}::sorted_list"
  initialize_document(@list_key, { :sorted_list => [], :last_updated => Time.now.utc.to_i, :user_key => @key })
  self      
end

Instance Attribute Details

#sort_keyObject

Returns the value of attribute sort_key.



8
9
10
# File 'lib/couchbase_structures/sorted_list.rb', line 8

def sort_key
  @sort_key
end

#sort_typeObject

Returns the value of attribute sort_type.



8
9
10
# File 'lib/couchbase_structures/sorted_list.rb', line 8

def sort_type
  @sort_type
end

Instance Method Details

#add(value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/couchbase_structures/sorted_list.rb', line 42

def add(value)
  doc = get_document(@list_key)
  list = doc["sorted_list"]
  list << value
  
  # simple sort only for singular values, string, integer, float, etc.
  list.sort!
  
  # complex sort for Hash types
  
  doc["sorted_list"] = list
  doc["last_updated"] = Time.now.utc.to_i
  replace_document(@list_key, doc)
  list
end

#deleteObject



68
69
70
# File 'lib/couchbase_structures/sorted_list.rb', line 68

def delete
  delete_document(@list_key)
end

#inspect(html = false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/couchbase_structures/sorted_list.rb', line 28

def inspect(html=false)
  if html
    str = "["
    self.items.each do |item|
      str += "<br />&nbsp;&nbsp;&nbsp;&nbsp;" + item.inspect
    end
    str += "<br />]"
    
    return "<strong>key</strong> = #{@key}    <br /><strong>items</strong> = #{str}"
  else
    return items.inspect
  end
end

#itemsObject



58
59
60
61
# File 'lib/couchbase_structures/sorted_list.rb', line 58

def items
  doc = get_document(@list_key)
  doc["sorted_list"]
end

#sizeObject



63
64
65
66
# File 'lib/couchbase_structures/sorted_list.rb', line 63

def size
  doc = get_document(@list_key)
  doc["sorted_list"].size
end