Class: UsefulDB::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/usefuldb/utilities.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/usefuldb/utilities.rb', line 10

def data
  @data
end

.dbpathObject

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_tagsObject



127
128
129
# File 'lib/usefuldb/utilities.rb', line 127

def all_tags
  @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(options = {})
  if options[:db]
    File.expand_path(options[:db])
  elsif options[:test]
    File.expand_path(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, options = {})
  @dbpath = db_path(options)

  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, options = {})
  @dbpath ||= db_path(options)
  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, options = {})
  @dbpath = db_path(options)

  if File.exist?(@dbpath)
    dbLoad(log, options)
  else
    @data = {
      "version" => UsefulDB::Version.to_s,
      "db" => []
    }
  end
end

.entriesObject



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_dataObject



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:)
  normalized_tags = normalize_tags(tags)

  @data["db"].each_with_index do |entry, id|
    if entry["value"] == value && normalize_tags(entry["tag"]) == normalized_tags
      return id
    end
  end

  nil
end

.get_entry(id) ⇒ Object

Raises:



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 normalize_tags(tags)
  Array(tags).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(tags, match: :all)
  tags = normalize_tags(tags)
  return entries if tags.empty?

  @data["db"].each_with_index.filter_map do |entry, id|
    entry_with_id(id, entry) if entry_matches?(entry, tags, 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.expand_path(File.join(__dir__, "../../resources/db.yaml"))
    FileUtils.cp(seed_path, resource_dir)
    log.debug "Database copied to ~/.usefuldb/db.yaml"
  end
end