Class: ParseConfig

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

Overview

Note: A group is a set of parameters defined for a subpart of a config file

Constant Summary collapse

Version =
'0.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ 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”



33
34
35
36
37
38
39
40
41
42
# File 'lib/parseconfig.rb', line 33

def initialize(config_file=nil)
  @config_file = config_file
  @params = {}
  @groups = []

  if(self.config_file)
    self.validate_config()
    self.import_config()
  end
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



25
26
27
# File 'lib/parseconfig.rb', line 25

def config_file
  @config_file
end

#groupsObject

Returns the value of attribute groups.



25
26
27
# File 'lib/parseconfig.rb', line 25

def groups
  @groups
end

#paramsObject

Returns the value of attribute params.



25
26
27
# File 'lib/parseconfig.rb', line 25

def params
  @params
end

Instance Method Details

#add(param_name, value) ⇒ Object

This method adds an element to the config object (not the config file) By adding a Hash, you create a new group



130
131
132
133
# File 'lib/parseconfig.rb', line 130

def add(param_name, value)
  self.instance_variable_set("@#{param_name}", value)
  self.params["#{param_name}"] = value
end

#add_to_group(group, param_name, value) ⇒ Object

Add parameters to a group. Note that parameters with the same name could be placed in different groups



137
138
139
140
# File 'lib/parseconfig.rb', line 137

def add_to_group(group, param_name, value)
  self.instance_eval("@#{group}[\"#{param_name}\"] = \"#{value}\"")
  self.params["#{group}"]["#{param_name}"] = value
end

#get_groupsObject

List available sub-groups of the config.



105
106
107
# File 'lib/parseconfig.rb', line 105

def get_groups()
  return self.groups
end

#get_paramsObject

This method returns all parameters/groups defined in a config file.



100
101
102
# File 'lib/parseconfig.rb', line 100

def get_params()
  return self.params.keys
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.



95
96
97
# File 'lib/parseconfig.rb', line 95

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

#import_configObject

Import data from the config to our config object.



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
82
83
84
85
86
87
88
89
90
# File 'lib/parseconfig.rb', line 54

def import_config()
  # The config is top down.. anything after a [group] gets added as part
  # of that group until a new [group] is found.  
  group = nil
  open(self.config_file).each do |line| 
    line.strip!
    unless (/^\#/.match(line))
      if(/\s*=\s*/.match(line))
        param, value = line.split(/\s*=\s*/, 2)  
        var_name = "#{param}".chomp.strip
        value = value.chomp.strip
        new_value = ''
        if (value)
          if value =~ /^['"](.*)['"]$/
            new_value = $1
          else
            new_value = value
          end
        else
          new_value = ''
        end 

        if group
          self.add_to_group(group, var_name, new_value)
        else
          self.add(var_name, new_value)
        end
        
      elsif(/^\[(.+)\]$/.match(line).to_a != [])
        group = /^\[(.+)\]$/.match(line).to_a[1]
        self.add(group, {})
        self.groups.push(group)
        
      end
    end
  end   
end

#nil_value(param) ⇒ Object

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

DEPRECATED - will be removed in future versions.



124
125
126
# File 'lib/parseconfig.rb', line 124

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.

DEPRECATED - will be removed in future versions.



115
116
117
# File 'lib/parseconfig.rb', line 115

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

#validate_configObject

Validate the config file, and contents



45
46
47
48
49
50
51
# File 'lib/parseconfig.rb', line 45

def validate_config()  
  if !File.readable?(self.config_file)
    raise Errno::EACCES, "#{self.config_file} is not readable" 
  end
  
  # FIX ME: need to validate contents/structure?
end

#write(output_stream = STDOUT) ⇒ Object

Writes out the config file to output_stream



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/parseconfig.rb', line 143

def write(output_stream=STDOUT)
  self.params.each do |name,value| 
    if value.class.to_s != 'Hash'
      if value.scan(/\w+/).length > 1
        output_stream.puts "#{name} = \"#{value}\""
      else
        output_stream.puts "#{name} = #{value}"
      end
    end
  end
  output_stream.puts "\n"
  
  self.groups.each do |group|
    output_stream.puts "[#{group}]"
    self.params[group].each do |param, value| 
      if value.scan(/\w+/).length > 1
        output_stream.puts "#{param} = \"#{value}\""
      else
        output_stream.puts "#{param} = #{value}"
      end
    end
    output_stream.puts "\n"
  end
end