在 MVC 中存储之前进行解析
我开始解析数据并从用户提供的字符串中获取一些结构(主要是提取数字和城市名称)。
我已经在 ruby 解释器中运行了一些代码,现在我想在 Web 应用程序中使用相同的代码。
我正在努力思考我的解析应该在代码中的什么位置,或者它的结构如何。
我最初的直觉是它属于模型,因为它是数据逻辑。例如,条目是否有一个整数,是否有两个整数,是否有城市名称等等。
但是,我的模型需要继承 ActiveRecord 和 Parslet(用于解析),而 Ruby 显然不需要不允许多重继承。
我当前的模型看起来像这样
#concert model require 'parslet' class concert < Parlset::Parser attr_accessible :date, :time, :city_id, :band_id, :original_string rule(:integer) {match('[0-9]').repeat(1)} root(:integer) end
确实不多,但我认为我被困住了,因为我的结构错误并且不知道如何连接这两部分。
我试图存储原始字符串以及解析数据的组成部分。
I'm getting started with parsing data and getting some structure from user supplied strings (mostly pulling out digits and city names).
I've run a bit of code in the ruby interpreter, and now I want to use that same code in a web application.
I'm struggling as to where in the code my parsing should be, or how it is structured.
My initial instinct was that it belongs in the model, because it is data logic. For example, does the entry have an integer, does it have two integers, does it have a city name, etc. etc.
However, my model would need to inherit both ActiveRecord, and Parslet (for the parsing), and Ruby apparently doesn't allow multiple inheritance.
My current model is looking like this
#concert model require 'parslet' class concert < Parlset::Parser attr_accessible :date, :time, :city_id, :band_id, :original_string rule(:integer) {match('[0-9]').repeat(1)} root(:integer) end
Really not much there, but I think I'm stuck because I've got the structure wrong and don't know how to connect these two pieces.
I'm trying to store the original string, as well as components of the parsed data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你想要的是:
I think what you want is:
在我看来,好像您需要多个解析器(一个用于城市名称,一个用于数字)。我建议您为此类解析器创建一个非正式接口,例如
然后您将创建几个 Ruby 类,每个类在 ./lib 中执行解析任务。
然后在模型中,您需要所有这些 ruby 类,并将它们放入任务中,比如说在 before_save 挂钩等中。
作为 Parslet 的作者,我可能会补充一点,解析数字或城市名称可能不是 Parslet 的最佳选择。可能要考虑那里的正则表达式。
It looks to me as though you need several parsers (one for city names, one for digits). I would suggest that you create an informal interface for such parsers, such as
Then you would create several Ruby classes that each do a parse task in ./lib.
Then in the model, you'd require all these ruby classes, and put them to the task, lets say in a before_save hook or such.
As the author of parslet, I might add that parsing digits or city names is probably not the sweet spot for parslet. Might want to consider regular expressions there.