Robert Feldt
10/31/2004 5:06:00 PM
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...
/Robert