Class: Omega::Config

Inherits:
Object show all
Defined in:
lib/omega/server/config.rb

Overview

Omega configuration manager, loads configuration options from config files, allowing the user to specify default values.

Config options can be accessed directly through instances of this class and consist of key (symbol) / value pairs.

Constant Summary collapse

CONFIG_FILES =

Omega config files in the order that they are loaded, eg options in later entries in the array with override those in previous entries

['/etc/omega.yml', '~/.omega.yml', './omega.yml']
CONFIG_CLASSES =

Omega classes which define the 'set_config' method taking an instance of Omega::Config containing configuration options to set

[Cosmos::RJR, Manufactured::RJR,
Missions::RJR, Stats::RJR,
Users::RJR, Users::Registry,
Users::EmailHelper, Omega::Server::ProxyNode]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Initialize the configuration, specifying config options


59
60
61
62
# File 'lib/omega/server/config.rb', line 59

def initialize(data={})
  @data = {}
  update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Return or set value of the config option specified by the name

of a missing method invoked on the config object

FIXME want to invoke methods such as enumerator methods (collect,each) on object itself


119
120
121
122
123
124
125
# File 'lib/omega/server/config.rb', line 119

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Class Method Details

.load(defaults = {}) ⇒ Object

Instantiate new Config instance and load values from the config files


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/omega/server/config.rb', line 40

def self.load(defaults = {})
  c = Config.new defaults

  CONFIG_FILES.each { |f|
    begin
      ff = File.expand_path(f)
      if File.file?(ff)
        c.update! YAML.load(File.open(ff))
      end
    rescue Exception => e
    end
  }

  c
end

Instance Method Details

#[](key) ⇒ Object

Return value for the specified key


91
92
93
# File 'lib/omega/server/config.rb', line 91

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object

Set the value of the specified config option


99
100
101
102
103
104
105
# File 'lib/omega/server/config.rb', line 99

def []=(key, value)
  if value.class == Hash
    @data[key.to_sym] = Config.new(value)
  else
    @data[key.to_sym] = value
  end
end

#has_attributes?(*required_attributes) ⇒ true, false

Return boolean indicating if the config has the specified options and they are not nil / blank.


69
70
71
72
73
74
75
# File 'lib/omega/server/config.rb', line 69

def has_attributes?(*required_attributes)
  # TODO multilevel / hash
  required_attributes.flatten.each { |attr|
    return false if !@data.has_key?(attr) || @data[attr].nil? || @data[attr] == ""
  }
  return true
end

#set_config(classes = CONFIG_CLASSES) ⇒ Object

Set local config on Omega classes specified by CONFIG_CLASSES above. If specified only the classes given will have their config set.


132
133
134
135
136
137
# File 'lib/omega/server/config.rb', line 132

def set_config(classes = CONFIG_CLASSES)
  classes.each { |kls|
    kls.set_config(self)
  }
  self
end

#to_hObject

Convert config to hash


108
109
110
111
112
# File 'lib/omega/server/config.rb', line 108

def to_h
  cp = Hash[@data]
  cp.each { |k,v| cp[k] = v.to_h if v.is_a?(Config) }
  cp
end

#update!(data) ⇒ Object

Update the config with the specified data


81
82
83
84
85
# File 'lib/omega/server/config.rb', line 81

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end