Module: JavaProperties::PropFile

Defined in:
lib/java_properties/properties-files.rb

Overview

PropFile allows reading and writing properties files

props = JavaProperties::PropFile.read( file )

JavaProperties::PropFile.write( file, props, "Optional header comment" )

JavaProperties::PropFile.append( file, props, "Optional seperator comment" )

Class Method Summary collapse

Class Method Details

.append(file, props, seperator = nil) ⇒ Object

Appends a Hash of key/value paris to the end of an existing properties file. Optionally a seperator comment can be provided.



46
47
48
49
50
51
# File 'lib/java_properties/properties-files.rb', line 46

def self.append file, props, seperator = nil
  unless seperator.nil? then
	File.open(file,'a') { |f| f.write "\n# " + seperator }
  end
  File.open(file,'a') { |f| f.write "\n" + self.stringify(props) }
end

.read(file) ⇒ Object

Reads in a Java properties file and returns the contents as a string for parsing.



21
22
23
24
25
26
27
28
# File 'lib/java_properties/properties-files.rb', line 21

def self.read file
  file = open(file)
  text = ''
  while(line = file.gets) do
	text += line
  end
  text
end

.write(file, props, header = nil) ⇒ Object

Writes out a Hash of key/value pairs to a properities file. Optionally a header comment can be provided.



33
34
35
36
37
38
39
40
# File 'lib/java_properties/properties-files.rb', line 33

def self.write file, props, header = nil
  unless header.nil? then
	File.open(file,'w') { |f| f.write "# " + header +"\n" }
    File.open(file,'a') { |f| f.write self.stringify(props) }
else
    File.open(file,'w') { |f| f.write self.stringify(props) }
  end
end