Class: ParseConfig

### Home Classes Methods

### In Files

- lib/parseconfig.rb

### Parent

Object

### Methods

- ::new
- #==
- #[]
- #add
- #add_to_group
- #eql?
- #get_groups
- #get_params
- #get_value
- #import_config
- #validate_config
- #write

### Class/Module Index

Quicksearch

- ParseConfig

No matching classes.

# ParseConfig

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

### Constants

Version

### Attributes

config_file[RW]
groups[RW]
params[RW]

### Public Class Methods

new(config_file=nil) click to toggle source

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"

    # 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

### Public Instance Methods

==(other) click to toggle source
Alias for: eql?
[](param) click to toggle source

This method is a shortcut to accessing the @params variable

    # File lib/parseconfig.rb, line 111
    def [](param)
      return self.params[param]
    end

add(param_name, value) click to toggle source

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

    # File lib/parseconfig.rb, line 127
    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) click to toggle source

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

    # File lib/parseconfig.rb, line 150
    def add_to_group(group, param_name, value)
      if ! self.groups.include?(group)
        self.add(group, {})
      end
      self.params[group][param_name] = value
    end

eql?(other) click to toggle source

Public: Compare this ParseConfig to some other ParseConfig. For two config to be equivalent, they must have the same sections with the same parameters

other - The other ParseConfig.

Returns true if ParseConfig are equivalent and false if they differ.

    # File lib/parseconfig.rb, line 190
    def eql?(other)
      self.params == other.params && self.groups == other.groups
    end

Also aliased as: ==
get_groups() click to toggle source

List available sub-groups of the config.

    # File lib/parseconfig.rb, line 121
    def get_groups()
      return self.groups
    end

get_params() click to toggle source

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

    # File lib/parseconfig.rb, line 116
    def get_params()
      return self.params.keys
    end

get_value(param) click to toggle source

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

    # File lib/parseconfig.rb, line 104
    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() click to toggle source

Import data from the config to our config object.

    # 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_with_index do |line, i|
        line.strip!

        # force_encoding not available in all versions of ruby
        begin
          if i.eql? 0 and line.include?("\xef\xbb\xbf".force_encoding("UTF-8"))
            line.delete!("\xef\xbb\xbf".force_encoding("UTF-8"))
          end
        rescue NoMethodError
        end

        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() click to toggle source

Validate the config file, and contents

    # File lib/parseconfig.rb, line 43
    def validate_config()
      unless 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, quoted=true) click to toggle source

Writes out the config file to output_stream

    # File lib/parseconfig.rb, line 158
    def write(output_stream=STDOUT, quoted=true)
      self.params.each do |name,value|
        if value.class.to_s != 'Hash'
          if quoted == true
            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 quoted == true
            output_stream.puts "#{param} = \"#{value}\""
          else
            output_stream.puts "#{param} = #{value}"
          end
        end
        output_stream.puts "\n"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.