From: "Ara.T.Howard" Date: 2005-10-29T01:28:06+09:00 Subject: Re: "Reflecting" on my self.n00b On Fri, 28 Oct 2005, swille wrote: > I'm admittedly quite a novice with programming. I'm sort of playing around > with some ideas, and I'm wondering if what I'm doing is completely stupid, > and if not, is there a better way to do it? Also, is there any way that I > could do some type of type-ish validation on the data? > > Basically, I want to provide what I suppose would be abstract class (?), > which someone else could extend and add attributes if they'd like. I'd like > to be able to look into the class and pick out the attributes to populate > (which is the reason for all of the sp_set & sp_get stuff). Basically, my > thought is to be able to provide a web interface at some point to populate > the needed data of a class, and then maybe Marshal.dump the object to a > database and call it later using that data as config data essentially. I'm > sure I could learn a ton about this from reading Rails code, but there's so > much of it that I don't know where to look. Output and code follows. > Please feel free to tell me if this is dumb :O) i think what you want is traits: harp:~ > cat a.rb require 'traits' require 'readline' class ServiceProfile trait 'url', 'type' => String, 'case' => %r|^http://| trait 'username', 'type' => String trait 'password', 'type' => String trait 'certificate', 'munge' => 'to_i', 'case' => Fixnum trait 'owner', 'type' => String def inspect "#{ self.class }( " << klass.reader_traits.map{|t| "#{ t }:#{ send(t).inspect}" }.join(', ') << " )" end def input io klass::writer_traits.each{|t| send t, Readline::readline("#{ t } ")} end def klass self.class end end class MyService < ServiceProfile trait 'extra', 'munge' => 'to_f', 'ducktypes' => %w( floor ceil ) end my_service = MyService::new p my_service my_service.input STDIN p my_service harp:~ > ruby a.rb MyService( extra:nil, certificate:nil, owner:nil, password:nil, url:nil, username:nil ) extra= 42.0 certificate= 42abcd owner= me password= tru5t url= http://codeforpeople.com/lib/ruby/traits/ username= ahoward MyService( extra:42.0, certificate:42, owner:"me", password:"tru5t", url:"http://codeforpeople.com/lib/ruby/traits/", username:"ahoward" ) regards. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | anything that contradicts experience and logic should be abandoned. | -- h.h. the 14th dalai lama ===============================================================================