key-db 4.0.1 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/keydb.rb +107 -83
- metadata +7 -7
- data/Diagram.jpg +0 -0
data/lib/keydb.rb
CHANGED
@@ -1,99 +1,123 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module KeyDB
|
2
|
+
class DB
|
3
|
+
# Makes filename and version into attributes
|
4
|
+
attr_reader :filename, :version
|
5
|
+
# Global version variable
|
6
|
+
$KEYDB_VERSION = "5.0.0"
|
7
|
+
|
8
|
+
def initialize filename
|
9
|
+
# Makes sure the filename ends in .db
|
10
|
+
@filename = filename.split(".")
|
11
|
+
@filename = "#{@filename[0]}.db"
|
12
|
+
# Checks if file exists
|
13
|
+
unless Dir.entries('.').detect {|f| f.match "#{@filename}"}
|
14
|
+
@db_file = File.new(@filename, "w")
|
15
|
+
@db_file.close
|
16
|
+
end
|
10
17
|
end
|
11
|
-
end
|
12
18
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
def change_key keyname, value
|
20
|
+
@db_file = File.open(@filename, "a")
|
21
|
+
# Writes a changekey sequence
|
22
|
+
@db_file.write(Marshal.dump("CK-***-#{keyname}-***-#{value}"))
|
23
|
+
@db_file.write("\n")
|
24
|
+
@db_file.close
|
25
|
+
end
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
def delete_key keyname
|
28
|
+
@db_file = File.open(@filename, "a")
|
29
|
+
# Writes a deletekey sequence
|
30
|
+
@db_file.write(Marshal.dump("DK(---)#{keyname}"))
|
31
|
+
@db_file.write("\n")
|
32
|
+
@db_file.close
|
33
|
+
end
|
26
34
|
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
def to_s
|
36
|
+
# Returns a string representation
|
37
|
+
return "KeyDB-#{@filename}"
|
38
|
+
end
|
30
39
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
40
|
+
def keys
|
41
|
+
# Creates a new array of keys to ignore
|
42
|
+
to_ignore = Array.new
|
43
|
+
File.open(@filename).each_line do |line|
|
44
|
+
if Marshal.load(line).split("(---)")[1] != nil
|
45
|
+
# Pushes deleted keys
|
46
|
+
to_ignore.push(Marshal.load(line).split("(---)")[1])
|
47
|
+
elsif Marshal.load(line).split("-***-")[1] != nil
|
48
|
+
# Pushes changed keys
|
49
|
+
to_ignore.push(Marshal.load(line).split("-***-")[1])
|
50
|
+
end
|
38
51
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
52
|
+
puts to_ignore
|
53
|
+
File.open(@filename).each_line do |line|
|
54
|
+
line = Marshal.load(line)
|
55
|
+
@keys_and_vals = line.split('///')
|
56
|
+
# Checks if key is ignored
|
57
|
+
if line.split("(---)").length == 2 or to_ignore.include? line.split("///")[0]
|
58
|
+
print
|
59
|
+
elsif to_ignore.include? line.split("-***-")[1]
|
60
|
+
print
|
61
|
+
else
|
62
|
+
# Prints valid keys
|
63
|
+
puts @keys_and_vals[0]
|
64
|
+
end
|
50
65
|
end
|
51
66
|
end
|
52
|
-
end
|
53
67
|
|
54
|
-
|
55
|
-
|
56
|
-
|
68
|
+
def delete_db
|
69
|
+
# Deletes the database
|
70
|
+
system("rm #{@filename}")
|
71
|
+
end
|
57
72
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
73
|
+
def wipe_db
|
74
|
+
# Creates a new, clean database
|
75
|
+
delete_db
|
76
|
+
File.new(@filename, "w")
|
77
|
+
end
|
62
78
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
if Marshal.load(line).split("///")[0] == keyname
|
74
|
-
raise "Keynames must be unique"
|
79
|
+
def method_missing(keyname, *value)
|
80
|
+
# Converts the keyname (method) to a string
|
81
|
+
keyname = keyname.to_s
|
82
|
+
# Checks if it is an accessor method
|
83
|
+
if keyname =~ /=/
|
84
|
+
keyname.gsub!("=", "")
|
85
|
+
value = value[0]
|
86
|
+
# Validates the keyname
|
87
|
+
if keyname =~ /\/\/\/|\(---\)|-\*\*\*-/
|
88
|
+
raise "Invalid Keyname!"
|
75
89
|
return
|
76
90
|
end
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
else
|
84
|
-
File.open(@filename, "r").each_line do |line|
|
85
|
-
if Marshal.load(line).split("(---)")[1] == keyname
|
86
|
-
return "Key has been deleted"
|
87
|
-
elsif Marshal.load(line).split("-***-")[1] == keyname
|
88
|
-
return Marshal.load(line).split("-***-")[2]
|
91
|
+
File.open(@filename, "r").each_line do |line|
|
92
|
+
if Marshal.load(line).split("///")[0] == keyname
|
93
|
+
# Checks key for uniqueness
|
94
|
+
raise "Keynames must be unique"
|
95
|
+
return
|
96
|
+
end
|
89
97
|
end
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
98
|
+
@db_file = File.open(@filename, "a")
|
99
|
+
# Writes key
|
100
|
+
@db_file.write(Marshal.dump("#{keyname}///#{value}"))
|
101
|
+
@db_file.write("\n")
|
102
|
+
@db_file.close
|
103
|
+
else
|
104
|
+
File.open(@filename, "r").each_line do |line|
|
105
|
+
# Checks if key has been deleted
|
106
|
+
if Marshal.load(line).split("(---)")[1] == keyname
|
107
|
+
return "Key has been deleted"
|
108
|
+
# Checks for a changekey sequence
|
109
|
+
elsif Marshal.load(line).split("-***-")[1] == keyname
|
110
|
+
return Marshal.load(line).split("-***-")[2]
|
111
|
+
end
|
95
112
|
end
|
96
|
-
|
97
|
-
|
113
|
+
File.open(@filename, "r").each_line do |line|
|
114
|
+
line = Marshal.load(line)
|
115
|
+
# Returns unchanged, undeleted keys
|
116
|
+
if line.split("///")[0] == keyname
|
117
|
+
return line.split("///")[1]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
98
122
|
end
|
99
|
-
end
|
123
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: key-db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,19 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: ! 'A database acessible via keys.
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
description: ! 'A database acessible via keys. Wrapped everything in a module. To
|
15
|
+
create a database: KeyDB::DB.new(''your database name''). Made attributes for all
|
16
|
+
database keys. Database keys now creatable and accessible via: db.foo = ''bar'';
|
17
|
+
db.foo. Cannot change keys through attributes, you must use change_key. Better documentation
|
18
|
+
available at http://keydbdocs.tumblr.com. File to require: ''keydb'''
|
18
19
|
email: slmnwise@gmail.com
|
19
20
|
executables: []
|
20
21
|
extensions: []
|
21
22
|
extra_rdoc_files: []
|
22
23
|
files:
|
23
24
|
- lib/keydb.rb
|
24
|
-
- Diagram.jpg
|
25
25
|
homepage: http://keydbdocs.tumblr.com/
|
26
26
|
licenses: []
|
27
27
|
post_install_message:
|
data/Diagram.jpg
DELETED
Binary file
|