From: Zach Dennis Date: 2003-12-18T13:15:26+09:00 Subject: Re: Configuration Files John, What type of environment would you think something like this would apply to? I dont' know what the "previous concept" was and it is hard for me to get a good grasp in your post. Please help me understand. =) Thanks, Zach -----Original Message----- From: John W. Long [mailto:ws@johnwlong.com] Sent: Wednesday, December 17, 2003 11:01 PM To: ruby-talk ML Subject: Configuration Files A while back someone submitted some code to this list for evaluating Configuration files in the style of: % cat test.config editor.spacespertab = 4 toolbar.visible = true statusbar.visible = false .. . . One of the great things about their code was that it enabled you to use ruby code within your configuration file so you could do things like this: %cat test2.config timer.alarm = 30 * 60 # set the timer to go off in 30 min Their implementation was pretty simple. Mainly they created a hash, splitting each line on /=/, and evaled the right side and stored the result in the hash with a key of the left side. I like this concept, but I wanted a better implementation. So I set to work and created the attached files. The attached now enables you to do this: %cat myprog.rb require 'config' $conf = Config.new # set up the default values and configuration structure: $conf.define %{ editor.spacespertab = 8 toolbar.visible = false toolbar.caption = "Tools" statusbar.visible = true copyright = "Copyright © 2003, John W. Long" } $conf.read File.new('test.config', 'r').read # you can even read multi line ruby snippets: $conf.read %{ copyright = "Copyright © 2003, John W. Long All Rights Reserved" alarm = 30 * 60 #seconds } # now for getting the values, note the object hierarchy toolbar.caption = $conf.toolbar.caption.value toolbar.show if $conf.toolbar.visible.value aboutbox.copyright = $conf.copyright.value .. . . You get the idea. A couple of advantages that this has over the previous concept: -- You define the default values and create the config structure at the same time -- Because you define a config structure if someone makes a typo in the config file misspelling "toolbar" as "tolbar" an error will be thrown (eventually it will even tell you what line the error was on) -- You access the values from the config file in almost the same way that they are written into the config file on your Config object I didn't attach the unit tests of this version because the code still relatively crude. Once I get things worked out a bit I may post it on RubyForge. Feedback is appreciated. ___________________ John Long www.wiseheartdesign.com