I am using a third party library (gem) to instantiate two versions of the same Class. The gem’s documentation instantiates the Class
client.sobject_module = My::Module
client.materialize("Contact") #=> My::Module::Contact
And my code does this.
client.sobject_module = MyModule
client.materialize("Contact")
The module itself is empty
module MyModule
end
There are two versions of the same code, yet when I do
MyModule::Contact.find("John")
for instance it appears the Contact got materialed into the global namespace. This is a materialize method that gets called from the gem
def materialize(classnames)
classes = (classnames.is_a?(Array) ? classnames : [classnames]).collect do |clazz|
original_classname = clazz
clazz = original_classname[0,1].capitalize + original_classname[1..-1]
unless const_defined_in_module(module_namespace, clazz)
new_class = module_namespace.const_set(clazz, Class.new(Databasedotcom::Sobject::Sobject))
new_class.client = self
new_class.materialize(original_classname)
new_class
else
module_namespace.const_get(clazz)
end
end
classes.length == 1 ? classes.first : classes
end
client on the new_class appears to get set correctly, but when I do
MyModule::Contact.client
it returns the same client as
MyModule1::Contact.client
I have spent days trying to figure this out any pointers will be helpful. The gem I am using is this.
I just released a wrapper around the gem that let’s you walk around this issue.
It works kinda like:
It’s currently on an early stage but I hope it suit’s your needs.
The source is available at https://github.com/sagmor/databasedotcom-isolated