使用 YAML 的 Ruby 构建器模式

发布于 2024-09-27 02:55:50 字数 772 浏览 0 评论 0原文

我的项目中现在有一个 Builder 模式的实例。目前,支持的输出格式是 CSV,但我现在想包含 YAML。很容易,我想。我拥有所有支持代码来更改类型。

我发现自己的处境有点复杂。使用Builder模式的目的是逐步构建输出文件。对我来说,这似乎与 YAML 直接矛盾 - 将所有对象放入数组并调用 YAML::dump()。

好消息是我确实有一系列这些对象。它被传递给主任。 这是Director 的construct() 方法的一个片段。

  def construct(aDataAccessObjectCollection)
    @daos = aDataAccessObjectCollection
    result = ''

    @daos.each do |dao|
      @builder.build_food_name(dao.get_property('name'))
      @builder.build_calories(dao.get_property('calories'))
      @builder.build_keywords(dao.get_property('keywords'))

      result += (@builder.get_result + "\n")
      @builder.flush
    end

    File.open(@file_name, 'w') do |file|
      file.write(result)
    end
  end

我不确定如何能够同时适应 CSV 和 YAML 格式。有什么想法吗?

I have an instance of the Builder pattern in my project right now. Currently, the supported output format is CSV, however I would now like to include YAML. Easy, I thought. I have all of the supporting code in to make the type change.

I'm finding myself to be in a bit of a complex. The intent of using the Builder pattern was to construct the output file step by step. To me, this seems directly contradictory to YAML - Get all of your objects into an array and call YAML::dump().

The good news is that I do have an array of those objects. It gets passed into the Director.
Here's a snippet from the construct() method of the Director.

  def construct(aDataAccessObjectCollection)
    @daos = aDataAccessObjectCollection
    result = ''

    @daos.each do |dao|
      @builder.build_food_name(dao.get_property('name'))
      @builder.build_calories(dao.get_property('calories'))
      @builder.build_keywords(dao.get_property('keywords'))

      result += (@builder.get_result + "\n")
      @builder.flush
    end

    File.open(@file_name, 'w') do |file|
      file.write(result)
    end
  end

I'm not sure how to be able to accommodate for both the CSV and YAML formats. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

丑丑阿 2024-10-04 02:55:50

我建议要小心,不要陷入过去所谓的“分析瘫痪”。模式很有用,直到它们使您需要做的事情变得更加困难。用韦恩的世界的话来说,“我说投掷!” - 或者更具体地说,将对象数组投掷到 YAML 并获取输出。换句话来说,“如果你的代码喷涌而出并且得到了输出,那么你就是黄金。但如果该模式没有给你带来任何好处,那么它就不是命中注定的”。

看看 - 模式是伟大的想法之一 - 它们为如何处理通常处理不好的常见情况提供了实用指导。他们可以帮助改进代码。他们可以帮助使代码(我敢说吗?)变得漂亮。但是,你知道,有时候你必须卷起袖子,捂住鼻子,尽一切努力来完成工作。此外,如果你“只是去做”,你可能会深入了解如何“做得更好” - 同时你会得到一些有用的东西。

聚会于...

I suggest being careful not to fall into what used to be called "paralysis by analysis". Patterns are useful, right up to the point where they make doing what you need to do more difficult. In the words of Wayne's World, "I say hurl!" - or more specifically, hurl the array of objects to YAML and get your output. To paraphrase badly, "If your code spews and you get your output, you're golden. But if the pattern gets you nothing, it was never meant to be".

Look - patterns are one of the Great Ideas - they provide practical guidance on how to handle common situations which are often handled badly. They can help make code better. They can help make code (dare I say it?) beautiful. But, y'know, there's times when you have to roll up your sleeves, hold your nose, and just do what it takes to get the job done. Besides, if you "just do it" you may gain insight on how to "just do it better" - and in the meantime you'll have something that works.

Party on...

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