I'm new to Ruby and I'd like to be able to derive from a class and
define a new class variable to replace a class variable in the parent
class -- however I also want the original class to have access to
it's original class variable.
Here's my simple test program:
class Klass
cattr_accessor :prefix
@@prefix = ''
def Klass.show(string)
puts @@prefix + string
end
end
class DerivedKlass < Klass
cattr_accessor :prefix
@@prefix = ''
end
Klass.prefix = "123"
Klass.show("abc") # => "123abc"
DerivedKlass.prefix = "789"
DerivedKlass.show("abc")# => "789abc"
Klass.show("abc") # => "789abc"
I'd like Klass.show("abc") to instead produce "123abc".
The reason I'd think I'd like to do it this way is that I need to
change how ActiveRecord works. Here's my problem:
I have 2 different ActiveRecord connections open and one of them is
to a shared database so I set the following class variable:
ActiveRecord::Base.table_name_prefix = 'myapp_'
So while I refer to the table 'users' in my code in the database the
table is actually named 'myapp_users'.
However the other connection is to a legacy database and I need to
access the tables in this database without the table_name_prefix.
If this can't be done perhaps I could do something in DerivedKlass so
that any accesses to it's actual or inherited object methods save
Klass.prefix, replace it, and restore it? Of course the restoration
would have to work for both normal program flow and for an error.
This sounds uglier to me.
Thanks for any advice.
--
- Stephen Bannasch
Concord Consortium, http://www.concord.org