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.
Files changed (3) hide show
  1. data/lib/keydb.rb +107 -83
  2. metadata +7 -7
  3. data/Diagram.jpg +0 -0
@@ -1,99 +1,123 @@
1
- class KeyDB
2
- attr_reader :filename, :version
3
- $KEYDB_VERSION = "4.0.1"
4
- def initialize filename
5
- @filename = filename.split(".")
6
- @filename = "#{@filename[0]}.db"
7
- unless Dir.entries('.').detect {|f| f.match "#{@filename}"}
8
- @db_file = File.new(@filename, "w")
9
- @db_file.close
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
- def change_key keyname, value
14
- @db_file = File.open(@filename, "a")
15
- @db_file.write(Marshal.dump("CK-***-#{keyname}-***-#{value}"))
16
- @db_file.write("\n")
17
- @db_file.close
18
- end
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
- def delete_key keyname
21
- @db_file = File.open(@filename, "a")
22
- @db_file.write(Marshal.dump("DK(---)#{keyname}"))
23
- @db_file.write("\n")
24
- @db_file.close
25
- end
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
- def to_s
28
- return "KeyDB-#{@filename}"
29
- end
35
+ def to_s
36
+ # Returns a string representation
37
+ return "KeyDB-#{@filename}"
38
+ end
30
39
 
31
- def keys
32
- to_ignore = Array.new
33
- File.open(@filename).each_line do |line|
34
- if Marshal.load(line).split("(---)")[1] != nil
35
- to_ignore.push(Marshal.load(line).split("(---)")[1])
36
- elsif Marshal.load(line).split("-***-")[1] != nil
37
- to_ignore.push(Marshal.load(line).split("-***-")[1])
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
- end
40
- puts to_ignore
41
- File.open(@filename).each_line do |line|
42
- line = Marshal.load(line)
43
- @keys_and_vals = line.split('///')
44
- if line.split("(---)").length == 2 or to_ignore.include? line.split("///")[0]
45
- print
46
- elsif to_ignore.include? line.split("-***-")[1]
47
- print
48
- else
49
- puts @keys_and_vals[0]
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
- def delete_db
55
- system("rm #{@filename}")
56
- end
68
+ def delete_db
69
+ # Deletes the database
70
+ system("rm #{@filename}")
71
+ end
57
72
 
58
- def wipe_db
59
- delete_db
60
- File.new(@filename, "w")
61
- end
73
+ def wipe_db
74
+ # Creates a new, clean database
75
+ delete_db
76
+ File.new(@filename, "w")
77
+ end
62
78
 
63
- def method_missing(keyname, *value)
64
- keyname = keyname.to_s
65
- if keyname =~ /=/
66
- keyname.gsub!("=", "")
67
- value = value[0]
68
- if keyname =~ /\/\/\/|\(---\)|-\*\*\*-/
69
- raise "Invalid Keyname!"
70
- return
71
- end
72
- File.open(@filename, "r").each_line do |line|
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
- end
78
- @db_file = File.open(@filename, "a")
79
- @to_write = (Marshal.dump("#{keyname}///#{value}"))
80
- @db_file.write(@to_write)
81
- @db_file.write("\n")
82
- @db_file.close
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
- end
91
- File.open(@filename, "r").each_line do |line|
92
- line = Marshal.load(line)
93
- if line.split("///")[0] == keyname
94
- return line.split("///")[1]
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
- end
97
- end
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.0.1
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-19 00:00:00.000000000 Z
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: ! 'A database acessible via keys. Made attributes for all database keys.
15
- Database keys now creatable and accessible via: db.foo = ''bar''; db.foo. Cannot
16
- change keys through attributes, you must use change_key. Better documentation available
17
- at http://keydbdocs.tumblr.com. File to require: ''keydb'''
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:
Binary file