Class: UsefulDB::UsefulUtils
- Inherits:
-
Object
- Object
- UsefulDB::UsefulUtils
- Defined in:
- lib/usefuldb/utilities.rb
Class Attribute Summary collapse
-
.data ⇒ Object
Returns the value of attribute data.
-
.dbpath ⇒ Object
Returns the value of attribute dbpath.
Class Method Summary collapse
-
.add(hash, log) ⇒ Object
Add an element to the database.
- .addColour(text, colour_code) ⇒ Object
-
.array_to_s(a) ⇒ Object
Convert an Array to a string.
- .blue(text) ⇒ Object
-
.count(log) ⇒ Object
Return the number of elements in the database.
-
.dbLoad(log) ⇒ Object
Load the database from disk.
-
.dbSave(log) ⇒ Object
Save the database to disk.
- .green(text) ⇒ Object
-
.list(log) ⇒ Object
List out all elements in the DB.
- .red(text) ⇒ Object
-
.remove(key, log) ⇒ Object
Remove an element from the database.
-
.search(tag, log) ⇒ Object
Search for a tag in the DB.
-
.setup(log) ⇒ Object
Setup the system for the first time.
- .yellow(text) ⇒ Object
Class Attribute Details
.data ⇒ Object
Returns the value of attribute data.
10 11 12 |
# File 'lib/usefuldb/utilities.rb', line 10 def data @data end |
.dbpath ⇒ Object
Returns the value of attribute dbpath.
10 11 12 |
# File 'lib/usefuldb/utilities.rb', line 10 def dbpath @dbpath end |
Class Method Details
.add(hash, log) ⇒ Object
Add an element to the database
56 57 58 |
# File 'lib/usefuldb/utilities.rb', line 56 def add(hash, log) if @data["db"].include?(hash) then raise EntryInDB, "Entry already in the DB"; else @data["db"] << hash; end end |
.addColour(text, colour_code) ⇒ Object
13 14 15 |
# File 'lib/usefuldb/utilities.rb', line 13 def addColour(text, colour_code) "\e[#{colour_code}m#{text}\e[0m" end |
.array_to_s(a) ⇒ Object
Convert an Array to a string
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/usefuldb/utilities.rb', line 112 def array_to_s(a) msg = '' index = 0 msg += "[" a.each do |i| if index == 0 msg += "\"" + i + "\"" else msg += ", \"" + i + "\"" end index += 1 end msg += "]" return msg end |
.blue(text) ⇒ Object
20 |
# File 'lib/usefuldb/utilities.rb', line 20 def blue(text); addColour(text, 34); end |
.count(log) ⇒ Object
Return the number of elements in the database.
46 47 48 49 50 51 52 |
# File 'lib/usefuldb/utilities.rb', line 46 def count(log) if @data["db"].count == 0 raise EmptyDB, "The DB is currently empty." else return @data["db"].count end end |
.dbLoad(log) ⇒ Object
Load the database from disk
37 38 39 40 41 42 |
# File 'lib/usefuldb/utilities.rb', line 37 def dbLoad(log) log.debug @dbpath = File.(File.dirname(__FILE__) + '/../../resources/db.yaml') @dbpath = ENV['HOME'] + "/.usefuldb/db.yaml" UsefulDB::Settings.load(@dbpath, log) @data = UsefulDB::Settings.data end |
.dbSave(log) ⇒ Object
Save the database to disk
25 26 27 28 29 30 31 32 33 |
# File 'lib/usefuldb/utilities.rb', line 25 def dbSave(log) if @dbpath.nil? log.debug @dbpath = File.(File.dirname(__FILE__) + '/../../resources/db.yaml') @dbpath = ENV['HOME'] + "/.usefuldb/db.yaml" UsefulDB::Settings.save(@data, @dbpath, log) else UsefulDB::Settings.save(@data, @dbpath, log) end end |
.green(text) ⇒ Object
18 |
# File 'lib/usefuldb/utilities.rb', line 18 def green(text); addColour(text, 32); end |
.list(log) ⇒ Object
List out all elements in the DB
106 107 108 |
# File 'lib/usefuldb/utilities.rb', line 106 def list(log) return @data["db"] end |
.red(text) ⇒ Object
17 |
# File 'lib/usefuldb/utilities.rb', line 17 def red(text); addColour(text, 31); end |
.remove(key, log) ⇒ Object
Remove an element from the database
62 63 64 65 66 67 68 69 70 |
# File 'lib/usefuldb/utilities.rb', line 62 def remove(key, log) if @data["db"].count == 0 raise EmptyDB, "You cannot call the remove function on an empty Database!" elsif @data["db"].count <= key || key < 0 raise KeyOutOfBounds, "Key is out of bounds and therefore does not exist in the DB" else @data["db"].delete_at(key) end end |
.search(tag, log) ⇒ Object
Search for a tag in the DB
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/usefuldb/utilities.rb', line 91 def search(tag, log) msg = "Searching the database for tag: " + tag + "\n" @data["db"].each do |db| if db["tag"].include?(tag) msg += "- Tags: " + array_to_s(db["tag"]) + "\n" msg += "- Value: " + db["value"] + "\n" msg += "- Description: " + db["description"] +"\n##\n" end end return msg end |
.setup(log) ⇒ Object
Setup the system for the first time
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/usefuldb/utilities.rb', line 74 def setup(log) log.debug "Checking to see if the database is already installed" resourceDir = ENV['HOME'] + "/.usefuldb/" log.debug resourceDir if File.directory?(resourceDir) log.debug "The folder already exists, do nothing" else log.debug "Creating ~/.usefuldb/ and installing the DB there." FileUtils.mkdir(resourceDir) dbpath = File.(File.dirname(__FILE__) + '/../../resources/db.yaml') FileUtils.cp(dbpath, resourceDir) log.debug "Database copied to ~/.usefuldb/db.yaml" end end |
.yellow(text) ⇒ Object
19 |
# File 'lib/usefuldb/utilities.rb', line 19 def yellow(text); addColour(text, 33); end |