[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

AppConfig, a configuration container class

martinus

10/31/2004 3:46:00 PM

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/ap...

Any comments? At first I thought this looks pretty ugly, but it is
actually quite useful.

martinus

4 Answers

Robert Feldt

10/31/2004 5:06:00 PM

0

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




Robert Feldt

10/31/2004 5:08:00 PM

0

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




martinus

10/31/2004 7:19:00 PM

0

Oh, I was not aware of ostruct
Which probably renders my implementation useless
But thanks, anyway ;-)

martinus

gabriele renzi

11/1/2004 12:51:00 AM

0

martinus ha scritto:


> Any comments? At first I thought this looks pretty ugly, but it is
> actually quite useful.

it is nice, thanks for sharing.
I'd change initialize slightly like this:

def initialize
@methods = Hash.new
if block_given?
yield self
fixate
else
@fixed = false
end
end

so that you could write:

config = AppConfig.new do |ac|
ac.window.name = "SuperDuper"
ac.app.version = "2.1.3"
ac.this.is.automatically.created = "blabla"
end
config.widnow #Error!