在 MVC 中存储之前进行解析

发布于 2024-11-19 09:26:19 字数 591 浏览 5 评论 0原文

我开始解析数据并从用户提供的字符串中获取一些结构(主要是提取数字和城市名称)。

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2024-11-26 09:26:19

我想你想要的是:

#concert model
require 'parslet'
class concert < ActiveRecord::Base
  before_save :parse_fields
  attr_accessible :date, :time, :city_id, :band_id, :original_string


   rule(:integer) {match('[0-9]').repeat(1)}
   root(:integer)

  private
  def parse_fields
    date = Parlset::Parser.method_on_original_string_to_extract_date
    time = Parlset::Parser.method_on_original_string_to_extract_time
    city_id = Parlset::Parser.method_on_original_string_to_extract_city_id
    band_id = Parlset::Parser.method_on_original_string_to_extract_band_id
  end
end

I think what you want is:

#concert model
require 'parslet'
class concert < ActiveRecord::Base
  before_save :parse_fields
  attr_accessible :date, :time, :city_id, :band_id, :original_string


   rule(:integer) {match('[0-9]').repeat(1)}
   root(:integer)

  private
  def parse_fields
    date = Parlset::Parser.method_on_original_string_to_extract_date
    time = Parlset::Parser.method_on_original_string_to_extract_time
    city_id = Parlset::Parser.method_on_original_string_to_extract_city_id
    band_id = Parlset::Parser.method_on_original_string_to_extract_band_id
  end
end
一笔一画续写前缘 2024-11-26 09:26:19

在我看来,好像您需要多个解析器(一个用于城市名称,一个用于数字)。我建议您为此类解析器创建一个非正式接口,例如

class Parser
  def parse(str) # returning result
  end 
end

然后您将创建几个 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

class Parser
  def parse(str) # returning result
  end 
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文