Module: Kodekopelli::PropertiesFile

Defined in:
lib/kodekopelli/properties_file.rb

Overview

This module encapsulates functionality related to manipulating properties files.

Please note that this module supports only the simplest types of properties files: those files whose entries are of the {key}={value} variety.

Class Method Summary collapse

Class Method Details

.to_hash(filename) ⇒ Object

Given the absolute or relative path to a properties file, returns a Hash containing the properties found.

:call-seq:

PropertiesFile.to_hash(filename) -> hash


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kodekopelli/properties_file.rb', line 16

def PropertiesFile.to_hash(filename)
  unless File.exists?(filename)
    raise("Unable to locate file #{filename}#")
  end
  
  prop_file = File.open(filename) 
  h = Hash.new
  begin
    prop_file.readlines.each { |current_line|
	  is_comment = (current_line.strip.index('#') == 0)
      equals_index = current_line.index("=")
	  
      unless is_comment || equals_index.nil?
        current_property = current_line[0, equals_index].strip 
 current_value = current_line[equals_index+1..current_line.chomp("\n").size-1]
 h[current_property] = current_value
      end
	  }
  ensure
    prop_file.close
  end
  h
end