Class: ParseConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/parseconfig.rb

Overview

This class was written to simplify the parsing of configuration files in the format of “param = value”. Please review the demo files included with this package.

For further information please refer to the ‘./doc’ directory as well as the ChangeLog and README files included.

Constant Summary collapse

Version =
'0.4.1'

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ ParseConfig

Initialize the class with the path to the ‘config_file’ The class objects are dynamically generated by the name of the ‘param’ in the config file. Therefore, if the config file is ‘param = value’ then the itializer will eval “@param = value”

Raises:

  • (Errno::EACCES)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/parseconfig.rb', line 28

def initialize(config_file)
  @config_file = config_file
  raise Errno::EACCES, "#{self.config_file} is not readable" unless File.readable?(self.config_file)
  open(self.config_file).each { |line| 
    line.chomp
    unless (/^\#/.match(line))
      if(/\s*=\s*/.match(line))
        param, value = line.split(/\s*=\s*/)	
        var_name = "#{param}".chomp.strip
        value = value.chomp.strip
        new_value = ''
        if (value)
          value.each_byte do |byte|
            new_value << byte.chr unless (byte.chr == "\'")
          end
        else
          new_value = ''
        end 
        self.instance_variable_set("@#{var_name}", new_value)
      end
    end
  }   
end

Instance Method Details

#config_fileObject



77
78
79
# File 'lib/parseconfig.rb', line 77

def config_file()
  @config_file
end

#config_file=(config_file) ⇒ Object



73
74
75
# File 'lib/parseconfig.rb', line 73

def config_file=(config_file)
  @config_file = config_file  
end

#get_value(param) ⇒ Object

This method will provide the value held by the object “@param” where “@param” is actually the name of the param in the config file.



55
56
57
# File 'lib/parseconfig.rb', line 55

def get_value(param)
  self.instance_variable_get("@#{param}") 
end

#nil_value(param) ⇒ Object

This method will set the value of ‘@param’ to nil (not in the config file, only in the app).



69
70
71
# File 'lib/parseconfig.rb', line 69

def nil_value(param)
  self.instance_variable_set("@#{param}", nil)
end

#override_value(param, value) ⇒ Object

This method is simple. Should you need to override a value dynamically, use override_value(param, value) where ‘param’ is the name of the paramater in the config file.



63
64
65
# File 'lib/parseconfig.rb', line 63

def override_value(param, value)
  self.instance_variable_set("@#{param}", value)
end