【问题标题】:Ruby Appending comment block to YAML fileRuby 将注释块附加到 YAML 文件
【发布时间】:2016-11-03 14:16:44
【问题描述】:

我有一个 yml 文件,用于存储我在版本之间添加的故事列表。

我正在使用 rake 任务根据我添加到此文件的故事动态更新版本号。

它正在引入一个新流程,因此我创建了以下评论块,这将帮助任何在这里评论的人以正确的格式添加故事:

#  Version control file.
#  Versions should be incremented as follows
#
#   [X - major change] . [V - new feature] . [I - Bug fix / Small change]
#
#  Update the undefined block with a one line readable description of what your story was about. example:
#
#  undefined:
#    stories:
#    - "I - fixed spelling mistake"
#    - "V - added import functionality"
#    - "X - rebuilt the main dashboard"
#

问题是在我的 rake 任务完成后,文件丢失了注释块。

我几乎加载了 YAML versions = YAML.load_file( 'doc/release.yml' ),然后在逻辑完成后我 File.open("doc/release.yml", 'w') { |f| YAML.dump(versions, f) }

versions 是新更新的哈希值。但是,这会删除文件的注释块。

我发现的其他解决方案只是修改现有的行。

有没有办法打开文件并添加上面的内容而不会弄乱下面的 YAML。任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby yaml


    【解决方案1】:

    不幸的是,由于转储而丢失 cmets 是很正常的。 你有两个选择:

    1. 将您的版本哈希转换为 yaml { :a => 'b'}.to_yaml,添加 cmets 并使用 File.write 自己进行转储,您可以覆盖正常 以这种方式在 YAML 中使用 .dump 方法
    2. 在 yaml 文件末尾将 cmets 分配给某个虚拟值 以便将它们读入版本并保存。

    【讨论】:

    • 谢谢小伙伴!我现在将其更改为 File.open('doc/release.yml','w') { |f| f.write comment_block; f.write versions.to_yaml }comment_block 方法只是返回注释块的字符串版本。这有效:)
    【解决方案2】:

    这是一个可能的解决方案。

    require 'yaml'
    
    versions_yaml = File.read('release.yml')
    versions = YAML.load(versions_yaml)
    comments = versions_yaml.scan(/^#.*?$/)
    
    File.open("release2.yml", 'w') { |f|
      f.puts comments
      YAML.dump(versions, f)
    }
    
    puts File.read("release2.yml")
    

    使用 release.yml :

    # I'm a comment on first line
    ---
    - 1
    - 2
    - 3
    # I'm a comment somewhere in the middle
    - - 4
      - 5
    

    它输出:

    # I'm a comment on first line
    # I'm a comment somewhere in the middle
    ---
    - 1
    - 2
    - 3
    - - 4
      - 5
    

    【讨论】:

    • 我真的很喜欢这种方法,因为它不会创建其他一些事实来源。 cmets 就是 cmets。
    【解决方案3】:

    我想出了这种在转储到 yaml 之前将 cmets 添加到我的 Hash 的方法。我使用它来初始化带有嵌入式 cmets 的配置文件,以记录配置选项。这是一个有限的解决方案。例如,没有阵列内 cmets。但它适用于简单的情况。

    cfg = {
      c: 'This is a comment',
      'foo' => 'bar',
      'level2' => {
        'level3' => {
          'foo' => 'bar',
          c1: 'This is a comment (line 1)',
          c2: 'This is a comment (line 2)',
          'foo2' => 'bar2',
        },
      },
    }
    
    YAML.dump(cfg).each_line do |l|
      if l.match(/:c(\d+)?:/)
        l.sub!(/:c(\d+)?:/, '#')
        l.sub!(/(^\s*# )["']/, '\1')
        l.sub!(/["']\s*$/, '')
      end
      puts l
    end
    

    生产:

    ---
    # This is a comment
    foo: bar
    level2:
      level3:
        foo: bar
        # This is a comment (line 1)
        # This is a comment (line 2)
        foo2: bar2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2012-08-19
      • 2018-08-27
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多