M
martinus
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.
You can get the code here:
http://martinus.geekisp.com/files/appconfig.rb
Any comments? At first I thought this looks pretty ugly, but it is
actually quite useful.
martinus
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.
You can get the code here:
http://martinus.geekisp.com/files/appconfig.rb
Any comments? At first I thought this looks pretty ugly, but it is
actually quite useful.
martinus