From: Robert Feldt Date: 2004-11-01T02:08:09+09:00 Subject: Re: AppConfig, a configuration container class Robert Feldt wrote: > martinus wrote: > >> Hi! I have written a class that I consider cool enough to share. The >> problem I tried to solve was that for an application I wrote, I wanted >> to have all configuration parameters centralized, and seperated from >> the normal code. At first I used a Hash with parameters names as >> strings, but this was cumbersome to use. I tried to find a convenient >> interface to a hierarchy of configuration parameters, and came up with >> an implementation that automatically defines accessors (actually, I >> only override method_missing). Have a look at this example: >> >> ac = AppConfig.new >> ac.window.name = "SuperDuper" >> ac.app.version = "2.1.3" >> ac.this.is.automatically.created = "blabla" >> >> After you have created all of your configuration parameters this way, >> to prevent typos when using the parameters, the configuration hierarchy >> can be fixated: >> >> ac.fixate >> >> After fixating, >> >> ac.widnow.name = "UberSuperDuper" >> >> generates a NoMethodError, because window was misspelled. Without >> fixating, this would have created a new setting. >> >> >> > It's nice; I've used this for similar situations: > > require 'ostruct' > class RecursiveOpenStruct < OpenStruct > def method_missing(method, *args) > super || send((method.to_s + "=").intern, RecursiveOpenStruct.new) > end > end > > but it doesn't behave exactly the same... > I forgot to say: but with freeze as a substitute for your fixate it probably behaves close enough. /R