class AppCfg::JavaProperties

JavaProperties class from github.com/axic/rsnippets/blob/master/javaproperties.rb (originally from devender.wordpress.com/2006/05/01/reading-and-writing-java-property-files-with-ruby/)

Consider splitting into own project and including as a gem. Some features missing: escape handling error handling, perhaps whitespace edge cases as well.

Consider checking for presence of JRuby and invoking java.util.Properties if available.

Attributes

filename[RW]
properties[RW]

Public Class Methods

new(filename) click to toggle source
# File lib/appcfg/sources/properties_source.rb, line 29
def initialize(filename)
  @filename = filename
  @properties = {}

  IO.foreach(@filename) do |line|
    @properties[$1.strip] = $2 if line = ~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/
  end
end

Public Instance Methods

add(key, value = nil) click to toggle source
# File lib/appcfg/sources/properties_source.rb, line 44
def add(key, value = nil)
  return unless key.length > 0
  @properties[key] = value
end
remove(key) click to toggle source
# File lib/appcfg/sources/properties_source.rb, line 49
def remove(key)
  return unless key.length > 0
  @properties.delete(key)
end
save() click to toggle source
# File lib/appcfg/sources/properties_source.rb, line 54
def save
  file = File.new(@filename, "w+")
  @properties.each { |key, value| file.puts "#{key}=#{value}\n" }
  file.close
end
to_s() click to toggle source
# File lib/appcfg/sources/properties_source.rb, line 38
def to_s
  output = "File name #{@file}\n"
  @properties.each { |key, value| output += " #{key} = #{value}\n" }
  output
end