【发布时间】:2011-07-05 14:50:32
【问题描述】:
我有一系列为底层 XML 建模的 Ruby 对象(如 OXM)。不幸的是,XML 正在更改,相应的版本正在更新。我需要更新我的 Ruby 对象才能处理这两个版本。我想要比我的方法中的大量 if/else 子句更干净的东西,因为这很可能会再次发生。有没有一种惯用的 Ruby 方法来处理这个问题?我正在考虑使用基类作为各种“版本化”类的代理,即
class XMLModel
class V1
# V1 specific implementation
end
class V2;
# V2 specific implementation
end
def initialize
# create a new V? and set up delegation to that specific version of the object
end
def from_xml(xml_string)
# use the XML string to determine correct version and return a
# specific version of the object
end
end
上述方法的好处是每个版本在代码中都是不同的,并且允许我添加/删除几乎没有向后兼容性测试的版本。坏事是我可能会得到很多代码重复。此外,在这种情况下,XMLModel.new 返回一个新的XMLModel,而XMLModel.from_xml 工厂方法返回一个新的XMLModel::V1。
想法?
【问题讨论】:
标签: ruby versioning idioms