Parent

Included Modules

Files

RConfig::PropertiesFile

Constants

COMMENT
KEYVAL
QUOTES
GROUP
NAMEGRP
KEY_REF

Public Class Methods

parse(config_file) click to toggle source

Parse config file and import data into hash to be stored in config.

     # File lib/rconfig/properties_file.rb, line 49
 49:     def self.parse(config_file)
 50:       raise ArgumentError, 'Invalid config file name.' unless config_file
 51: 
 52:       config = {}
 53: 
 54:       # The config is top down.. anything after a [group] gets added as part
 55:       # of that group until a new [group] is found.  
 56:       group, topgrp = nil
 57:       config_file.split("\n").each do |line|   # for each line in the config file
 58:         line.strip!
 59:         unless COMMENT.match(line) # skip comments (lines that state with '#')
 60:           if KEYVAL.match(line)    # if this line is a config property
 61:             key, val = line.split(KEYVAL, 2) # parse key and value from line
 62:             key = key.chomp.strip
 63:             val = val.chomp.strip
 64:             if val
 65:               if val =~ QUOTES # if the value is in quotes
 66:                 value = $1     # strip out value from quotes
 67:               else
 68:                 value = val    # otherwise, leave as-is
 69:               end
 70:             else
 71:               value = ''       # if value was nil, set it to empty string
 72:             end
 73: 
 74:             if topgrp                                # If there was a top-level named group, then there must be a group.
 75:               config[topgrp][group][key] = value     # so add the prop to the named group
 76:             elsif group                              # if this property is part of a group
 77:               config[group][key] = value             # then add it to the group
 78:             else                                     # otherwise... 
 79:               config[key] = value                    # add the prop to top-level config
 80:             end
 81: 
 82:           elsif match = NAMEGRP.match(line) # This line is a named group (i.e. [env "test"], [env "qa"], [env "production"])
 83:             topgrp, group = match.to_a[1..1] # There can be multiple groups within a single top-level group
 84:             config[topgrp] ||= {} # add group to top-level group
 85:             config[topgrp][group] ||= {} # add name of group as subgroup (properties are added to subgroup)
 86: 
 87:           elsif match = GROUP.match(line) # if this line is a config group
 88:             group = match.to_a[1] # parse the group name from line
 89:             topgrp = nil # we got a new group with no namespace, so reset topgrp
 90:             config[group] ||= {} # add group to top-level config
 91:           end
 92:         end
 93:       end
 94: 
 95:       # Pre-populate the values that refer to other properties
 96:       # Example:
 97:       #   root_path = /path/to/root
 98:       #   image_path = %{root_path}/images
 99:       config = parse_references(config)
100: 
101:       config # return config hash
102:     end
parse_references(hash, config=nil) click to toggle source

Recursively checks all the values in the hash for references to other properties, and replaces the references with the actual values. The syntax for referring to another property in the config file is the ley of the property wrapped in curly braces, preceded by a percent sign. If the property is in a group, then the full namespace must be used.

Example:

  root_path = /path/to/root             # In this property file snippet 
  image_path = %{root_path}/images      # image path refers to root path
     # File lib/rconfig/properties_file.rb, line 116
116:     def self.parse_references hash, config=nil
117:       config ||= hash.dup
118: 
119:       hash.each do |key, val|
120:         case val
121:         when Hash
122:           hash[key] = parse_references(val, config)
123:         when String
124:           pre, ref, post = KEY_REF.match(val).to_a[1..1]
125:           hash[key] = if ref
126:               ref = ref.split('.').inject(config){|c,i| c && c[i] }
127:               [pre, ref, post].join              
128:             else
129:               val
130:             end         
131:         end
132:       end
133: 
134:       hash
135:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.