Class: UsefulDB::Utils
- Inherits:
-
Object
- Object
- UsefulDB::Utils
- 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
- .all_tags ⇒ Object
- .array_to_s(array) ⇒ Object
- .count(_log) ⇒ Object
- .db_path(options = {}) ⇒ Object
- .dbLoad(log, options = {}) ⇒ Object
- .dbSave(log, options = {}) ⇒ Object
- .ensure_db!(log, options = {}) ⇒ Object
- .entries ⇒ Object
- .export_data ⇒ Object
- .find_entry_id(tags:, value:) ⇒ Object
- .get_entry(id) ⇒ Object
- .list(_log) ⇒ Object
- .normalize_tags(tags) ⇒ Object
- .remove(key, _log) ⇒ Object
- .search(tag, _log) ⇒ Object
- .search_entries(tags, match: :all) ⇒ Object
- .setup(log) ⇒ 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
60 61 62 63 64 65 66 |
# File 'lib/usefuldb/utilities.rb', line 60 def add(hash, _log) if @data["db"].include?(hash) raise EntryInDB, "Entry already in the DB" end @data["db"] << hash end |
.all_tags ⇒ Object
127 128 129 |
# File 'lib/usefuldb/utilities.rb', line 127 def @data["db"].flat_map { |entry| entry["tag"] }.uniq.sort end |
.array_to_s(array) ⇒ Object
143 144 145 |
# File 'lib/usefuldb/utilities.rb', line 143 def array_to_s(array) "[" + array.map { |item| "\"#{item}\"" }.join(", ") + "]" end |
.count(_log) ⇒ Object
56 57 58 |
# File 'lib/usefuldb/utilities.rb', line 56 def count(_log) @data["db"].count end |
.db_path(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/usefuldb/utilities.rb', line 12 def db_path( = {}) if [:db] File.([:db]) elsif [:test] File.(File.join(__dir__, "../../resources/db.yaml")) else File.join(Dir.home, ".usefuldb", "db.yaml") end end |
.dbLoad(log, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/usefuldb/utilities.rb', line 28 def dbLoad(log, = {}) @dbpath = db_path() unless File.exist?(@dbpath) raise EntryNotFound, "Database not found at #{@dbpath}" end UsefulDB::Settings.load(@dbpath, log) @data = UsefulDB::Settings.data end |
.dbSave(log, options = {}) ⇒ Object
22 23 24 25 26 |
# File 'lib/usefuldb/utilities.rb', line 22 def dbSave(log, = {}) @dbpath ||= db_path() FileUtils.mkdir_p(File.dirname(@dbpath)) UsefulDB::Settings.save(@data, @dbpath, log) end |
.ensure_db!(log, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/usefuldb/utilities.rb', line 39 def ensure_db!(log, = {}) @dbpath = db_path() if File.exist?(@dbpath) dbLoad(log, ) else @data = { "version" => UsefulDB::Version.to_s, "db" => [] } end end |
.entries ⇒ Object
93 94 95 96 97 |
# File 'lib/usefuldb/utilities.rb', line 93 def entries @data["db"].each_with_index.map do |entry, id| entry_with_id(id, entry) end end |
.export_data ⇒ Object
52 53 54 |
# File 'lib/usefuldb/utilities.rb', line 52 def export_data @data end |
.find_entry_id(tags:, value:) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/usefuldb/utilities.rb', line 115 def find_entry_id(tags:, value:) = () @data["db"].each_with_index do |entry, id| if entry["value"] == value && (entry["tag"]) == return id end end nil end |
.get_entry(id) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/usefuldb/utilities.rb', line 99 def get_entry(id) entry = @data["db"][id] raise KeyOutOfBounds, "Entry id #{id} does not exist" if entry.nil? entry_with_id(id, entry) end |
.list(_log) ⇒ Object
131 132 133 |
# File 'lib/usefuldb/utilities.rb', line 131 def list(_log) @data["db"] end |
.normalize_tags(tags) ⇒ Object
147 148 149 |
# File 'lib/usefuldb/utilities.rb', line 147 def () Array().flat_map { |tag| tag.to_s.split(",") }.map(&:strip).reject(&:empty?) end |
.remove(key, _log) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/usefuldb/utilities.rb', line 68 def remove(key, _log) if @data["db"].count == 0 raise EmptyDB, "You cannot call the remove function on an empty Database!" elsif key < 0 || key >= @data["db"].count raise KeyOutOfBounds, "Entry id #{key} does not exist" else @data["db"].delete_at(key) end end |
.search(tag, _log) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/usefuldb/utilities.rb', line 135 def search(tag, _log) search_entries([tag]).map do |entry| "- Tags: #{array_to_s(entry['tag'])}\n" \ "- Value: #{entry['value']}\n" \ "- Description: #{entry['description']}\n##\n" end.join end |
.search_entries(tags, match: :all) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/usefuldb/utilities.rb', line 106 def search_entries(, match: :all) = () return entries if .empty? @data["db"].each_with_index.filter_map do |entry, id| entry_with_id(id, entry) if entry_matches?(entry, , match) end end |
.setup(log) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/usefuldb/utilities.rb', line 78 def setup(log) resource_dir = File.join(Dir.home, ".usefuldb") log.debug "Checking to see if the database is already installed" if File.directory?(resource_dir) log.debug "The folder already exists, do nothing" else log.debug "Creating ~/.usefuldb/ and installing the DB there." FileUtils.mkdir(resource_dir) seed_path = File.(File.join(__dir__, "../../resources/db.yaml")) FileUtils.cp(seed_path, resource_dir) log.debug "Database copied to ~/.usefuldb/db.yaml" end end |