Class: Cocainum::Storage
- Inherits:
-
Object
- Object
- Cocainum::Storage
- Defined in:
- lib/cocainum/storage.rb
Instance Method Summary collapse
- #find(namespace, tags) ⇒ Object
-
#initialize ⇒ Storage
constructor
A new instance of Storage.
- #read(namespace, key) ⇒ Object
- #read_json(namespace, key) ⇒ Object
- #remove(namespace, key) ⇒ Object
- #set_tags(obj, tag_list) ⇒ Object
- #update(namespace, key, data, tags_list = []) ⇒ Object
- #write(namespace, key, value, tags = []) ⇒ Object
- #write_json(namespace, key, value, tags = []) ⇒ Object
Constructor Details
#initialize ⇒ Storage
7 8 9 |
# File 'lib/cocainum/storage.rb', line 7 def initialize @storage = Cocaine::Synchrony::Service.new('storage') end |
Instance Method Details
#find(namespace, tags) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/cocainum/storage.rb', line 59 def find(namespace, ) results = @storage.find(namespace, ).collect if results.size > 0 && !results[0].empty? results[0] else [] end end |
#read(namespace, key) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/cocainum/storage.rb', line 21 def read(namespace, key) results = @storage.read(namespace, key).collect if results.size > 0 && !results[0].empty? results[0] else '' end end |
#read_json(namespace, key) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/cocainum/storage.rb', line 30 def read_json(namespace, key) results = read(namespace, key) if results.empty? {} else JSON.parse(results, {symbolize_names: true}) end end |
#remove(namespace, key) ⇒ Object
68 69 70 71 72 |
# File 'lib/cocainum/storage.rb', line 68 def remove(namespace, key) @storage.remove(namespace, key).collect true end |
#set_tags(obj, tag_list) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/cocainum/storage.rb', line 49 def (obj, tag_list) = [] if tag_list.class == Array && obj.class == Hash tag_list.each {|tag| .push tag.to_s + '_' + obj[tag.to_sym].to_s } end end |
#update(namespace, key, data, tags_list = []) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/cocainum/storage.rb', line 39 def update(namespace, key, data, = []) if data.class == Hash obj = read_json namespace, key data.each { |k, value| obj[k.to_sym] = value } write_json namespace, key, obj, (obj, ) end end |
#write(namespace, key, value, tags = []) ⇒ Object
11 12 13 14 15 |
# File 'lib/cocainum/storage.rb', line 11 def write(namespace, key, value, = []) @storage.write(namespace, key, value, ).collect true end |
#write_json(namespace, key, value, tags = []) ⇒ Object
17 18 19 |
# File 'lib/cocainum/storage.rb', line 17 def write_json(namespace, key, value, = []) write(namespace, key, JSON.generate(value), ['_ct_json'] + ) end |