Module: OpenBEL::Config

Includes:
DotHash
Defined in:
app/openbel/api/config.rb,
lib/openbel/api/config/config.rb

Defined Under Namespace

Classes: SilentProperties

Constant Summary collapse

CFG_VAR =
'OPENBEL_SERVER_CONFIG'
PLUGIN_PATHS =
'plugin_paths'

Class Method Summary collapse

Class Method Details

.load(config_file = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/openbel/api/config/config.rb', line 10

def self.load(config_file=nil)
  config_file = ENV[CFG_VAR] || config_file
  fail %Q{The configuration file is empty.} unless config_file

  config = File.open(config_file, 'r:UTF-8') do |cf|
    {}.merge(YAML::load(cf))
  end

  plugin_manager = PluginManager.new
  plugin_manager.with_plugins_from([config.delete(PLUGIN_PATHS)].flatten.compact.map(&:to_s))
  plugins = plugin_manager.to_a

  errors = plugin_manager.check_configuration(plugins, config)

  if !errors.empty?
    fail RuntimeError.new(errors.join("\n"))
  end

  plugin_manager.configure_plugins(plugins.dup, config.dup)
end

.load!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/openbel/api/config.rb', line 10

def self.load!
  config_file = ENV[CFG_VAR] || raise('No OpenBEL API configuration found. Set the OPENBEL_API_CONFIG_FILE environment variable.')
  config = {}
  File.open(config_file, 'r:UTF-8') do |cf|
    config = YAML::load(cf)
    if not config
      config = {}
    end
  end
  cfg = Settings.new config, SilentProperties

  failure = validate cfg
  if failure
    if block_given?
      yield failure[1]
    else
      fail(<<-ERR.gsub(/^\s+/, ''))
        Configuration error within #{File.expand_path(config_file)}:
        #{failure[1]}
      ERR
    end
  end

  cfg
end