Just in case anyone is interested, I came up with a quick way to
support my other odbc databases inside Merb. These will be ones that
are not the Production or Development database. Obviously this is
pretty custom to what I'm doing, but could be tweaked for your own
purposes. I'm just sharing in case it inspires someone else to make
their own solution to a similar issue.
I created a DbFactory module that loaded up my odbc definitions in my
database.yml file and set them as constants. Now, I'm only looking
for the odbc connections, so others would probably need to adjust
that. I also added a model class that simply has a quick version of
to_xml and to_text.
module DbFactory
@config = Erubis.load_yaml_file(MERB_ROOT / "config" /
"database.yml")
@config.each do |c|
if c[1]['adapter'] == 'odbc'
DbFactory.const_set(c[0].upcase, Sequel.odbc(c[1]['dsn'],
:user => c[1]['username'], :password => c[1]['password']))
end
end
class Model < Sequel::Model
def to_xml
klass = self.class.to_s.downcase.gsub('::','-')
xml = "<#{klass}>\n"
self.values.each do |key,value|
xml += " <#{key}>#{value}</#{key}>\n"
end
xml += "</#{klass}>\n"
xml
end
alias to_text to_yaml
end
end
Now, I inherit from that class in my models and can call the constants
created inside DbFactory with set_dataset.
For example, say I created two dsn's (db1 and db2)
module Db1
class MyClass < DbFactory::Model
set_dataset(DbFactory::DB1[:sometable])
end
end
module Db2
class MyClass < DbFactory::Model
set_dataset(DbFactory::DB2[:sometable])
end
end
Now, I can call my classes on different databases by using the
namespace my modules created.
a = Db1::MyClass.find(1) # search on db1
b = Db2::MyClass.find(2) # search on db2
a.to_xml # output the model in xml format
b.to_text # output the model in yaml format
I'd love to hear better ways if anyone is bored. :)
Take care!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---