Class: ParseConfig
- Inherits:
-
Object
- Object
- ParseConfig
- 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 =
'1.0.2'
Instance Attribute Summary collapse
-
#config_file ⇒ Object
Returns the value of attribute config_file.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
-
#[](param) ⇒ Object
This method is a shortcut to accessing the @params variable.
-
#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.
-
#add_to_group(group, param_name, value) ⇒ Object
Add parameters to a group.
-
#get_groups ⇒ Object
List available sub-groups of the config.
-
#get_params ⇒ Object
This method returns all parameters/groups defined in a config file.
-
#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.
-
#import_config ⇒ Object
Import data from the config to our config object.
-
#initialize(config_file = nil) ⇒ ParseConfig
constructor
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.
-
#validate_config ⇒ Object
Validate the config file, and contents.
-
#write(output_stream = STDOUT) ⇒ Object
Writes out the config file to output_stream.
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”
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/parseconfig.rb', line 31 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_file ⇒ Object
Returns the value of attribute config_file.
23 24 25 |
# File 'lib/parseconfig.rb', line 23 def config_file @config_file end |
#groups ⇒ Object
Returns the value of attribute groups.
23 24 25 |
# File 'lib/parseconfig.rb', line 23 def groups @groups end |
#params ⇒ Object
Returns the value of attribute params.
23 24 25 |
# File 'lib/parseconfig.rb', line 23 def params @params end |
Instance Method Details
#[](param) ⇒ Object
This method is a shortcut to accessing the @params variable
102 103 104 |
# File 'lib/parseconfig.rb', line 102 def [](param) return self.params[param] end |
#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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/parseconfig.rb', line 118 def add(param_name, value) if value.class == Hash if self.params.has_key?(param_name) if self.params[param_name].class == Hash self.params[param_name].merge!(value) elsif self.params.has_key?(param_name) if self.params[param_name].class != value.class raise ArgumentError, "#{param_name} already exists, and is of different type!" end end else self.params[param_name] = value end if ! self.groups.include?(param_name) self.groups.push(param_name) end else self.params[param_name] = value end 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
141 142 143 144 145 146 |
# File 'lib/parseconfig.rb', line 141 def add_to_group(group, param_name, value) if ! self.groups.include?(group) self.add(group, {}) end self.params[group][param_name] = value end |
#get_groups ⇒ Object
List available sub-groups of the config.
112 113 114 |
# File 'lib/parseconfig.rb', line 112 def get_groups() return self.groups end |
#get_params ⇒ Object
This method returns all parameters/groups defined in a config file.
107 108 109 |
# File 'lib/parseconfig.rb', line 107 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.
DEPRECATED - will be removed in future versions
95 96 97 98 99 |
# File 'lib/parseconfig.rb', line 95 def get_value(param) puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \ "config['param'] or config['group']['param'] instead." return self.params[param] end |
#import_config ⇒ Object
Import data from the config to our config object.
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 82 83 84 85 86 87 |
# File 'lib/parseconfig.rb', line 52 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) { |f| f.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, {}) end end end } end |
#validate_config ⇒ Object
Validate the config file, and contents
43 44 45 46 47 48 49 |
# File 'lib/parseconfig.rb', line 43 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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/parseconfig.rb', line 149 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 |