[Nitro] Building objects from Strings

gabriele renzi surrender_it at yahoo.it
Wed Aug 9 04:41:24 EDT 2006

Hi everyone,

I have a somewhat working interface to change Settings from the 
administration UI, and I think it is a Good Thing(TM).

But bat the moment it is slightly fragile, and uses a hacky way of 
getting the values from the user.
Basically, I'm providigin small text fields for each "good" setting, and 
in the server side I'm assigning to the setting the value by 
transforming it in the right type.

In code:

  setting.value = setting.type.new(param[name])

Now to the bad one: ruby does not have String constructors for many 
things, so the above is working fine for String and Numerics.
I added the handling for Booleans as a simple
def TrueClass.new(arg) arg=='true' end
def FalseClass.new(arg) not TrueClass.new(arg) end

A better solution could be using YAML.load on the given String.
Someone has other ideas or could point me to something in facets (i.e. 
core/kernel/parse_literal) ?


blog en: http://www.riffraff.info
blog it: http://riffraff.blogsome.com
jabber : rff.rff at gmail dot com

More information about the Nitro-general mailing list