Class: ConfigParser
- Inherits:
-
Hash
- Object
- Hash
- ConfigParser
- Defined in:
- lib/elasticDynamoDb/configparser.rb
Instance Method Summary collapse
-
#initialize(fname = nil) ⇒ ConfigParser
constructor
A new instance of ConfigParser.
- #parse ⇒ Object
Constructor Details
#initialize(fname = nil) ⇒ ConfigParser
Returns a new instance of ConfigParser.
2 3 4 |
# File 'lib/elasticDynamoDb/configparser.rb', line 2 def initialize(fname = nil) @file_name = fname end |
Instance Method Details
#parse ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/elasticDynamoDb/configparser.rb', line 6 def parse begin input_source = File.open(@file_name, "r").each_line if @file_name rescue Exception => e puts "\e[31mError loading file: #{e}\e[0m\n\n" exit 1 end config = {} config_section = nil input_source.each_with_index.inject(config) {|acc, (line, index)| if line =~ /^\[(.+?)\]/ config_section = $1 acc[config_section] ||= {} elsif line =~ /^(#|;|\n)/ && config_section acc[config_section]["comment_#{index}"] = line elsif line =~ /^\s*(.+?)\s*[=:]\s*(.+)$/ && config_section # this returns a 2 parts $1 key, $2 value config_key = $1 config_value = $2 acc[config_section][config_key] = config_value else acc['pre_section'] ||= {} acc['pre_section']["comment_#{index}"] = line end acc } end |