Re: Need help understanding metaclassing
- From: Chris Czub <chris.czub@xxxxxxxxx>
- Date: Fri, 26 Oct 2007 07:19:36 +0900
Robert Dober wrote:
On 10/25/07, Chris Czub <chris.czub@xxxxxxxxx> wrote:
Why can't I use the attribute accessor? Rather - I understand why I
can't, because it was created in the User class rather than the
DefaultUser class, but how can I get access to it?
--
Posted via http://www.ruby-forum.com/.
class User
@users = {}
class << self
attr_reader :users
end
def initialize name
email = nil
loop do
email = "#{name}@#{rand 1000}"
break unless self.class.users[ email ]
end
self.class.users[ email ] = self
....
is this what you want?
R.
I don't really want to define it in the User class, because it might not
belong to a particular user... maybe I am not making sense.
I am writing a testing application for many web sites that require user
registrations. Each website requires similar users, but different ones
might have additional fields required of a user, so my idea is to create
a metaclass of user for each user configuration a website might require.
So, for a web site based in the UK, the metaclass looks like this:
class UKUser < User
attribs :address3, :i_own_or_work_for_a_small,
:my_small_business_has_its, :yes_please_send_me_the_mi, :postal
first_name( "John" )
last_name( "Doe" )
email( "john.doe#{rand(100)}@abc.com" )
address1( "1 Sunshine Parkway" )
address2( "" )
address3( "" )
city( "London" )
postal( "EH15 2JJ" )
gender( "male" )
phone( "(555)123-4567" )
age( "18-25" )
my_small_business_has_its( "no" )
i_own_or_work_for_a_small( "no" )
yes_please_send_me_the_mi( "no" )
end
So I am able to say
user = UKUser.new
user.address3("Apartment 12")
user.my_small_business_has_its("yes")
However, I cannot do this:
user.email("bob.smith@xxxxxxx")
Any ideas?
--
Posted via http://www.ruby-forum.com/.
.
- Follow-Ups:
- Re: Need help understanding metaclassing
- From: Drew Olson
- Re: Need help understanding metaclassing
- References:
- Need help understanding metaclassing
- From: Chris Czub
- Re: Need help understanding metaclassing
- From: Rick DeNatale
- Re: Need help understanding metaclassing
- From: Daniel Waite
- Re: Need help understanding metaclassing
- From: Daniel Waite
- Re: Need help understanding metaclassing
- From: Chris Czub
- Re: Need help understanding metaclassing
- From: Daniel Waite
- Re: Need help understanding metaclassing
- From: Chris Czub
- Re: Need help understanding metaclassing
- From: Daniel Waite
- Re: Need help understanding metaclassing
- From: Chris Czub
- Re: Need help understanding metaclassing
- From: Robert Dober
- Need help understanding metaclassing
- Prev by Date: Re: Need help understanding metaclassing
- Next by Date: Skip the first invocation e.g. skip_first { foo }
- Previous by thread: Re: Need help understanding metaclassing
- Next by thread: Re: Need help understanding metaclassing
- Index(es):
Relevant Pages
|