Class: TaliaUtil::Configuration::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/talia_util/configuration/config_file.rb

Overview

This is an object representation of a configuration file, the elements of the file can be directly accessed using dynamic getters/setters on the the class.

Direct Known Subclasses

DatabaseConfig

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ ConfigFile

Initialize from the given template



12
13
14
# File 'lib/talia_util/configuration/config_file.rb', line 12

def initialize(template)
  @config_doc = YAML::load_file(template)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object

Automatically creates “accessors” for the config properties.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/talia_util/configuration/config_file.rb', line 26

def method_missing(method, *params)
  method = method.to_s
  assign = method[-1..-1] == '=' # True if last char is a =
  
  result = nil
  
  if(assign)
    return super if(params.size != 1)
    result = @config_doc[method[0..-2]] = params[0]
  else
    return super if(params.size != 0)
    result = @config_doc[method]
  end
  
  result
end

Instance Method Details

#[](id) ⇒ Object



21
22
23
# File 'lib/talia_util/configuration/config_file.rb', line 21

def [](id)
  @config_doc[id]
end

#write(file) ⇒ Object

Write the configuration to the given file



17
18
19
# File 'lib/talia_util/configuration/config_file.rb', line 17

def write(file)
  open(file, 'w') { |io| io.puts(@config_doc.to_yaml) }
end