Class: UsefulDB::Settings

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/usefuldb/settings.rb', line 9

def data
  @data
end

Class Method Details

.autoConvert(log) ⇒ Object

Converts from db versions < 0.0.6 to the structure introduced in version 0.0.7



52
53
54
55
56
57
58
59
60
61
# File 'lib/usefuldb/settings.rb', line 52

def autoConvert(log)
  log.warn "autoConvert Method executing"
  convertedDB = {"version" => UsefulDB::Version.to_s, "db" => @data}
  
  convertedDB["db"].each do |element|
    element["description"] = "nil"
  end
  
  @data = convertedDB
end

.autoUpgrade(log) ⇒ Object

Convert the db from versions lower than the latest version of usefuldb to the latest version



65
66
67
68
69
70
71
72
73
# File 'lib/usefuldb/settings.rb', line 65

def autoUpgrade(log)
  log.warn "autoUpgrade Method executing"
  @data["version"] = UsefulDB::Version.to_s
  resource_path = File.join(File.dirname(__FILE__), "../../resources/db.yaml")
  @resourceData = YAML.load(File.open(resource_path))
  
  log.warn "Updating local database with default entries"
  @data["db"] = (@data["db"] | @resourceData["db"]).uniq
end

.load(path, log) ⇒ Object

Loads the database from disk



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/usefuldb/settings.rb', line 12

def load(path, log)
  begin
    @data = YAML.load(File.open(path))
    
    if @data.class == Array
      log.warn "Assuming currently installed DB version < 0.0.7 running autoConvert"
      
      autoConvert(log)
      log.warn "Database conversion successful"
      save(@data, path, log)
      log.debug "Saving the database"
    elsif @data["version"] != UsefulDB::Version.to_s
      log.warn "The database version is lower than the latest version running autoUpgrade"
      
      autoUpgrade(log)
      log.warn "Database upgrade successful"
      save(@data, path, log)
      log.debug "Saving the database"
    else
      log.debug "Database version is current"
    end
    
    log.debug @data.inspect
    
  rescue ArgumentError => e
    exit("Could not parse YAML: #{e.message}")
  end  
end

.save(newData, path, log) ⇒ Object

Saves the database to disk



43
44
45
46
47
48
# File 'lib/usefuldb/settings.rb', line 43

def save(newData, path, log)
  @data = newData
  f = File.open(path, "w")
  f.write(@data.to_yaml)
  f.close
end