Property files

Discussion in 'Ruby' started by Ron Coutts, Dec 23, 2003.

  1. Ron Coutts

    Ron Coutts Guest

    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
    Ron Coutts, Dec 23, 2003
    #1
    1. Advertising

  2. Ron Coutts

    John W. Long Guest

    ---- "Ron Coutts" wrote: ----
    > Is there a library class for handling common property files that have
    > the key=value syntax?


    There are several. I'm working on one:

    http://ruby-talk.org/88419
    ___________________
    John Long
    www.wiseheartdesign.com
    John W. Long, Dec 24, 2003
    #2
    1. Advertising

  3. "Ron Coutts" <> schrieb im Newsbeitrag
    news:000b01c3c99e$ee53f040$...
    > 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
    Robert Klemme, Dec 26, 2003
    #3
  4. Ron Coutts

    Andre Nathan Guest

    On Fri, 2003-12-26 at 09:06, Robert Klemme wrote:
    > 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
    Andre Nathan, Dec 26, 2003
    #4
  5. Ron Coutts

    Andre Nathan Guest

    On Fri, 2003-12-26 at 09:45, Andre Nathan wrote:
    > 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
    Andre Nathan, Dec 26, 2003
    #5
  6. Ron Coutts

    Guest

    Hi all!

    >> Is there a library class for handling common property files that have
    >> the key=value syntax?


    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.
    , Dec 30, 2003
    #6
  7. "Andre Nathan" <> schrieb im Newsbeitrag
    news:...
    > On Fri, 2003-12-26 at 09:06, Robert Klemme wrote:
    > > def method_missing(sym,*args)
    > > s = sym.to_s.freeze

    >
    > 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
    Robert Klemme, Jan 10, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,358
    Brock Allen
    Apr 4, 2005
  2. Joshua Beall
    Replies:
    1
    Views:
    414
    Bertilo Wennergren
    Dec 10, 2003
  3. Noozer
    Replies:
    10
    Views:
    2,042
    Mitja
    Oct 13, 2004
  4. Nathan Sokalski
    Replies:
    1
    Views:
    1,097
    =?Utf-8?B?SmVk?=
    Apr 29, 2006
  5. TS
    Replies:
    3
    Views:
    2,459
Loading...

Share This Page