I see several options for you.
You can request the proxy method on XMLModel using method_missing .
class XMLModel def load_xml(xml) version = determine_version(xml) case version when :v1 @model = XMLModelV1.new when :v2 @model = XMLModelV2.new end end def method_missing(sym, *args, &block) @model.send(sym, *args, &block) end end
Another option is to dynamically copy methods from a specific version to an XMLModel instance. However, I would not want to do this if it was not necessary.
The third option is to create a module for each version that has methods specific to that version. Then, instead of the proxy object, you simply enable the module for a specific version.
module XMLModelV1 #methods specific to v1 end module XMLModelV2 #methods specific to v2 end class XMLModel #methods common to both versions def initialize(version) load_module(version) end def load_xml(xml) load_module(determine_version(xml)) end private def load_module(version) case version when :v1 include XMLMOdelV1 when :v2 include XMLModelV2 end end end
source share