Class: SakaiInfo::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/database.rb

Constant Summary collapse

DEFAULT_CONFIG_FILE =
File.expand_path("~/.sakai-info")
@@default_database_name =
nil
@@config =
nil
@@databases =
{}
@@logger =

set global logger

nil

Class Method Summary collapse

Class Method Details

.configure(config) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sakai-info/database.rb', line 37

def self.configure(config)
  begin
    if config.is_a? Hash
      @@config = config
    elsif config.is_a? String and File.exist?(config)
      # try to parse as a filename first
      if File.exist?(config)
        @@config = YAML::load_file(config)
      end
    else
      # otherwise try to parse it generically
      @@config = YAML::load(config)
    end
  rescue Exception => e
    raise InvalidConfigException.new("Unable to parse configuration: #{e}")
  end
end

.connect(database_name = :default) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sakai-info/database.rb', line 68

def self.connect(database_name = :default)
  if @@config.nil?
    DB.load_config
  end
  if @@databases[database_name].nil?
    @@databases[database_name] =
      Database.new(if database_name == :default
                     if @@default_database_name.nil?
                       @@config[@@config.keys.first]
                     else
                       @@config[@@default_database_name]
                     end
                   else
                     @@config[database_name]
                   end,
                   @@logger)
  end
  @@databases[database_name].connect
end

.databasesObject



63
64
65
# File 'lib/sakai-info/database.rb', line 63

def self.databases
  @@config
end

.default_database=(database_name) ⇒ Object



88
89
90
# File 'lib/sakai-info/database.rb', line 88

def self.default_database=(database_name)
  @@default_database_name = database_name
end

.load_configObject



55
56
57
58
59
60
61
# File 'lib/sakai-info/database.rb', line 55

def self.load_config
  if File.readable? DEFAULT_CONFIG_FILE
    DB.configure(DEFAULT_CONFIG_FILE)
  else
    raise MissingConfigException.new("No config file found at #{DEFAULT_CONFIG_FILE}")
  end
end

.logger=(logger) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sakai-info/database.rb', line 94

def self.logger=(logger)
  @@logger = logger

  # also force it on any existing database connections
  @@databases.each do |name, dbconn|
    puts "updating #{name}"
    puts dbconn.class
    dbconn.logger = @@logger
    puts dbconn.connect.loggers.inspect
  end
end