简体   繁体   English

Rails模型的XML映射和验证

[英]XML mapping and validation for Rails models

I am implementing an application that manipulate XML documents using Ruby on Rails. 我正在实现一个使用Ruby on Rails处理XML文档的应用程序。 I want to have models that encapsulate all the logic and then convert them to corresponding XML document when save. 我想拥有封装所有逻辑的模型,然后在保存时将它们转换为相应的XML文档。 Although I do not need database persistence for my models, I want my models to have validation mechanism like ActiveRecord's. 尽管我的模型不需要数据库持久性,但我希望模型具有诸如ActiveRecord的验证机制。 Also, for converting between XML and Ruby objects, an XML mapping library is preferred to make things easier. 另外,为了在XML和Ruby对象之间进行转换,最好使用XML映射库以使事情变得容易。

Although there are quite a few solutions which allow using ActiveRecord without table, it seems to me the XML mapping libraries (eg ROXML , XML Mapping ) do not seem to play well with ActiveRecord'S fields. 尽管有很多解决方案允许不使用表使用ActiveRecord,但在我看来,XML映射库(例如ROXMLXML Mapping )似乎无法与ActiveRecord的S字段配合使用。 In other words, it does not look like that they can be used together due to the conflict it their syntax. 换句话说,由于语法上的冲突,它们似乎无法一起使用。

Therefore, I would like to know what are the preferred solution in this case. 因此,我想知道在这种情况下首选的解决方案。 The solution which allow the use of an XML binding library with tableless models with validation functionality. 该解决方案允许将XML绑定库与具有验证功能的无表模型一起使用。

For example, one solution is to have two separate models. 例如,一种解决方案是拥有两个单独的模型。 One is tableless ActiveRecord and the other is plain Ruby objects with xml binding (like what is described in this post ). 一个是无表的ActiveRecord,另一个是纯红宝石XML绑定(喜欢在描述对象这篇文章 )。 The ActiveRecord models are for validation. ActiveRecord模型用于验证。 To convert them to XML, it will need to be copies to XML binding models first. 要将它们转换为XML,首先需要将其复制到XML绑定模型。 Although this solution does work, it is not elegant. 尽管此解决方案确实有效,但它并不优雅。

You are saying that you don't need "database persistence", but then you say that you want to save them to an XML file, and read them from it. 您是在说不需要“数据库持久性”,而是在说要将它们保存到XML文件并从中读取它们。 So you need persistence - on a file. 因此,您需要持久性-在文件上。 The difference between that and "database persistence" is only linguistic, since we have SQLite, which stores the whole database on a file. 那和“数据库持久性”之间的区别只是语言上的,因为我们有SQLite,它将整个数据库存储在一个文件中。

What you want to do could be done by implementing your own adapter . 您可以通过实现自己的适配器来完成。 It would not be complicated, but it would take some time. 这不会很复杂,但是会花费一些时间。

I might made the question looks more complicated that it actually is. 我可能使问题看起来实际上更复杂了。 Essentially what I wanted are subset of ActiveRecord features (validations, create object from hash) and XML binding. 本质上,我想要的是ActiveRecord功能的子集(验证,通过哈希创建对象)和XML绑定。 In this case, it is probably better to use standard Ruby objects with libraries that provide comparable functionality to ActiveRecord. 在这种情况下,最好将标准Ruby对象与可提供与ActiveRecord相当功能的库一起使用。 For validation, validatable is the right fit. 对于验证, validateable是正确的选择。 Creating object from hash could also be easily implemented. 从哈希创建对象也可以轻松实现。

In summary, Ruby class + ROXML + validatable gem + custom hash-to-object implementation is the way to go for me. 总而言之,Ruby类+ ROXML +可验证的gem +自定义哈希到对象的实现是我的理想选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM