Property files

R

Ron Coutts

Is there a library class for handling common property files that have
the key=value syntax? Basically all I need to do is read some
properties from a properties file, perform succ! on some of them, and
write the property file back to disk. There were a couple of threads
about properties files earlier in December that mentioned use of
Config.rb but I'm having trouble finding documentation for this library,
if it is one. I'm using Ruby 1.8.0.

Ron
 
R

Robert Klemme

Ron Coutts said:
Is there a library class for handling common property files that have
the key=value syntax? Basically all I need to do is read some
properties from a properties file, perform succ! on some of them, and
write the property file back to disk. There were a couple of threads
about properties files earlier in December that mentioned use of
Config.rb but I'm having trouble finding documentation for this library,
if it is one. I'm using Ruby 1.8.0.

I made a posting some time ago, maybe that helps. The class can work with
nested names "foo.bar.baz='hello'":

class Config
def initialize
@values = {}
end

def method_missing(sym,*args)
s = sym.to_s.freeze

if s[-1] == ?=
# setter
@values = args.size == 1 ? args[0] : args
else
# getter
@values ||= self.class.new
end
end

def load(file)
instance_eval File.readlines(file, nil).shift
end

def self.load(file)
conf = self.new
conf.load file
conf
end
end

conf = Config.new

conf.load "conf.rc"
p conf

conf = Config.load "conf.rc"
p conf

Cheers

robert
 
A

Andre Nathan

def method_missing(sym,*args)
s = sym.to_s.freeze

Is there a need to freeze s here? If I don't freeze it, I can do this:
if s[-1] == ?=
# setter

s.sub!(/=$/, '')
@values = args.size == 1 ? args[0] : args
else


This, way, if the property file looks like

foo.name = 'Foo'

I can access the value with

conf = Config.load "conf.rc"
puts conf.values['foo'].values['id']

as opposed to

puts conf.values['foo'].values['id=']

Regards,
Andre
 
A

Andre Nathan

Is there a need to freeze s here? If I don't freeze it, I can do this:

Well, i could also do

key = s.sub(/$=/, '')
@values[key] = args.size == 1 ? args[0] : args

Anyway... is the freeze needed?

Andre
 
J

jrb3

Hi all!

I have a simple one accessible through acceptor.tigris.org/.
Download the initial tarball, and grab acceptor/lib/runtimeProps.rb
for your work.

Joseph Beckenbach
lead XP tester, Eidogen Inc.
 
R

Robert Klemme

Andre Nathan said:
Is there a need to freeze s here?

unfrozen Strings are dup'ed when put into a hash. That's why I put the
freeze there. But the freeze should be done later, when it's clear whether
it's a setter or a getter.
If I don't freeze it, I can do this:
if s[-1] == ?=
# setter

s.sub!(/=$/, '')
@values = args.size == 1 ? args[0] : args
else


This, way, if the property file looks like

foo.name = 'Foo'

I can access the value with

conf = Config.load "conf.rc"
puts conf.values['foo'].values['id']

as opposed to

puts conf.values['foo'].values['id=']


No. The idea of the whole thing is to be able to do

puts conf.foo.name

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top