AppConfig, a configuration container class

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
 
R

Robert Feldt

martinus said:
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
 
R

Robert Feldt

Robert said:
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
 
M

martinus

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

martinus
 
G

gabriele renzi

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top