Class: Can::Store
- Inherits:
-
Object
- Object
- Can::Store
- Defined in:
- lib/can/store.rb
Constant Summary collapse
- HEADER =
"Can"
- SEPARATOR =
"\n\n"
Instance Method Summary collapse
- #all ⇒ Object
- #decrypt(payload) ⇒ Object
- #encrypt(payload) ⇒ Object
- #exists(key) ⇒ Object
- #format ⇒ Object
- #get(key) ⇒ Object
-
#initialize(file, password) ⇒ Store
constructor
A new instance of Store.
- #migrate ⇒ Object
- #password(new_password) ⇒ Object
- #remove(key) ⇒ Object
- #rename(key, new_key) ⇒ Object
- #set(key, value) ⇒ Object
- #tag(key, tag) ⇒ Object
- #untag(key, tag) ⇒ Object
Constructor Details
#initialize(file, password) ⇒ Store
Returns a new instance of Store.
11 12 13 14 15 |
# File 'lib/can/store.rb', line 11 def initialize(file, password) @password = password @file = file @format = "1" end |
Instance Method Details
#all ⇒ Object
17 18 19 |
# File 'lib/can/store.rb', line 17 def all() read() end |
#decrypt(payload) ⇒ Object
81 82 83 84 |
# File 'lib/can/store.rb', line 81 def decrypt(payload) decoded = decode(payload) Crypto.decrypt(decoded, @password) end |
#encrypt(payload) ⇒ Object
76 77 78 79 |
# File 'lib/can/store.rb', line 76 def encrypt(payload) encrypted = Crypto.encrypt(payload, @password) encode(encrypted) end |
#exists(key) ⇒ Object
25 26 27 28 |
# File 'lib/can/store.rb', line 25 def exists(key) data = read() data[key] ? true : false end |
#format ⇒ Object
21 22 23 |
# File 'lib/can/store.rb', line 21 def format() @format end |
#get(key) ⇒ Object
30 31 32 33 |
# File 'lib/can/store.rb', line 30 def get(key) data = read() data[key] || nil end |
#migrate ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/can/store.rb', line 92 def migrate() count = 0 data = read() data.each do |key, value| # puts "Checking key #{key}..." if value.class != Hash data[key] = {} data[key]["value"] = value data[key]["created"] = Time.new data[key]["tags"] = [] count += 1 puts "Key #{key} migrated to new format" else puts "Key #{key} already exists in new format" end end write(data) count end |
#password(new_password) ⇒ Object
86 87 88 89 90 |
# File 'lib/can/store.rb', line 86 def password(new_password) data = read() @password = new_password write(data) end |
#remove(key) ⇒ Object
70 71 72 73 74 |
# File 'lib/can/store.rb', line 70 def remove(key) data = read() data.delete(key) write(data) end |
#rename(key, new_key) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/can/store.rb', line 44 def rename(key, new_key) data = read() data[new_key] = data[key] data.delete(key) write(data) end |
#set(key, value) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/can/store.rb', line 35 def set(key, value) data = read() data[key] ||= {} data[key]["value"] = value data[key]["created"] = Time.new data[key]["tags"] ||= [] write(data) end |
#tag(key, tag) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/can/store.rb', line 51 def tag(key, tag) data = read() data[key]["tags"] = data[key]["tags"] || [] if not data[key]["tags"].include?(tag) data[key]["tags"] << tag return write(data) end false end |
#untag(key, tag) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/can/store.rb', line 61 def untag(key, tag) data = read() if data[key]["tags"] and data[key]["tags"].include?(tag) data[key]["tags"].delete(tag) return write(data) end false end |