Class: Resat::Config
- Inherits:
-
Object
- Object
- Resat::Config
- Defined in:
- lib/config.rb
Constant Summary collapse
- DEFAULT_FILE =
'config/resat.yaml'
- DEFAULT_SCHEMA_DIR =
File.(File.join(File.dirname(__FILE__), '..', 'schemas'))
- DEFAULTS =
{ 'base_url' => '', 'use_ssl' => false, 'variables' => {} }
Instance Method Summary collapse
-
#initialize(filename, schemasdir) ⇒ Config
constructor
A new instance of Config.
- #method_missing(*args) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(filename, schemasdir) ⇒ Config
Returns a new instance of Config.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/config.rb', line 46 def initialize(filename, schemasdir) (self.methods - (Object.instance_methods + [ 'init', 'valid?', 'method_missing' ])).each { |m| self.class.send(:remove_method, m.to_sym) } schemafile = File.join(schemasdir || DEFAULT_SCHEMA_DIR, 'config.yaml') unless File.exists?(schemafile) Log.error("Missing configuration file schema '#{schemafile}'") @valid = false return end schema = Kwalify::Yaml.load_file(schemafile) validator = Kwalify::Validator.new(schema) parser = Kwalify::Yaml::Parser.new(validator) @valid = true @config = { 'use_ssl' => false, 'username' => nil, 'password' => nil, 'port' => nil } cfg_file = filename || DEFAULT_FILE config = parser.parse_file(cfg_file) if parser.errors.empty? if config.nil? Log.error("Configuration file '#{cfg_file}' is empty.") @valid = false else @config.merge!(config) # Dynamically define the methods to forward to the config hash @config.each_key do |meth| self.class.send(:define_method, meth) do |*args| @config[meth] || DEFAULTS[meth] end end end else errors = parser.errors.inject("") do |msg, e| msg << "#{e.linenum}:#{e.column} [#{e.path}] #{e.}\n\n" end Log.error("Configuration file '#{cfg_file}' is invalid:\n#{errors}") @valid = false end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
87 88 89 |
# File 'lib/config.rb', line 87 def method_missing(*args) nil end |
Instance Method Details
#valid? ⇒ Boolean
83 84 85 |
# File 'lib/config.rb', line 83 def valid? @valid end |