如何在 Ruby 中的 YAML 文件中包含 YAML 文件

发布于 2024-08-21 09:48:55 字数 386 浏览 10 评论 0原文

YAML 中是否有自定义标签供 ruby​​ 在 YAML 文件中包含 YAML 文件?

#E.g.:  
--- !include
filename: another.yml

前段时间有人提出了类似问题没有相关答案。

我想知道 Ruby 是否有一些类似于 Python 的 this 的自定义标签。

Is there a custom tag in YAML for ruby to include a YAML file inside a YAML file?

#E.g.:  
--- !include
filename: another.yml

A similar question was asked some time ago and there was no relevant answer.

I am wondering if there is some custom tag for Ruby similar to this one for Python.

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

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

发布评论

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

评论(7

酷遇一生 2024-08-28 09:48:55

如果您使用 Rails,YAML 可以包含 ERB。

将它们组合在一起,以下是如何使用 <%= %> 包含另一个文件中的一个文件:

database.yml

<% if File.exists?('/tmp/mysql.sock') %>
<%= IO.read('config/database.mysql.yml') %>
<% else %>
<%= IO.read('config/database.sqlite.yml') %>
<% end %>

database.sqlite.yml

sqlite: &defaults
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *defaults
  database: db/development.sqlite3

test:
  <<: *defaults
  database: db/test.sqlite3

production:
  <<: *defaults
  database: db/production.sqlite3

database.mysql.yml

development:
  adapter: mysql2
  # ... the rest of your mysql configuration ...

If you are in Rails, YAML can include ERB.

Combine that together, and here is how you can use <%= %> to include one file from another:

database.yml

<% if File.exists?('/tmp/mysql.sock') %>
<%= IO.read('config/database.mysql.yml') %>
<% else %>
<%= IO.read('config/database.sqlite.yml') %>
<% end %>

database.sqlite.yml

sqlite: &defaults
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *defaults
  database: db/development.sqlite3

test:
  <<: *defaults
  database: db/test.sqlite3

production:
  <<: *defaults
  database: db/production.sqlite3

database.mysql.yml

development:
  adapter: mysql2
  # ... the rest of your mysql configuration ...
往日情怀 2024-08-28 09:48:55

我找到了一种使用 ERB 来解决我的情况的方法。

我猴子修补了 YAML 模块以添加两个新方法,

module YAML
    def YAML.include file_name
      require 'erb'
      ERB.new(IO.read(file_name)).result
    end

    def YAML.load_erb file_name
      YAML::load(YAML::include(file_name))
    end  
end

我有三个 YAML 文件。

mod1_config.yml

mod1:
    age: 30
    city: San Francisco

mod2_config.yml

mod2:
    menu: menu1
    window: window1

all_config.yml

<%= YAML::include("mod1_config.yml") %>
<%= YAML::include("mod2_config.yml") %>

使用方法 YAML::load_erb 解析 yaml 文件,而不是方法 YAML::load

  config = YAML::load_erb('all_config.yml') 
  config['mod1']['age'] # 30
  config['mod2']['menu'] # menu1

注意事项:

  1. 不支持文档合并
  2. 最后包含覆盖相同名称的键

I found a way to address my scenario using ERB.

I monkey patched YAML module to add two new methods

module YAML
    def YAML.include file_name
      require 'erb'
      ERB.new(IO.read(file_name)).result
    end

    def YAML.load_erb file_name
      YAML::load(YAML::include(file_name))
    end  
end

I have three YAML files.

mod1_config.yml

mod1:
    age: 30
    city: San Francisco

mod2_config.yml

mod2:
    menu: menu1
    window: window1

all_config.yml

<%= YAML::include("mod1_config.yml") %>
<%= YAML::include("mod2_config.yml") %>

Parse the yaml file using the method YAML::load_erb instead of the method YAML::load.

  config = YAML::load_erb('all_config.yml') 
  config['mod1']['age'] # 30
  config['mod2']['menu'] # menu1

Caveats:

  1. Does not support document merge
  2. Last include overwrites same named keys
晨敛清荷 2024-08-28 09:48:55

如果您的目标是避免 YAML 文件中的重复(不一定包括外部文件),我建议您执行以下操作:

development: &default
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: db_dev
  pool: 5
  username: usr
  password: psw
  host: localhost
  port: 3306

dev_cache:
  <<: *default

new:
  <<: *default
  database: db_new

test:
  <<: *default
  database: db_test

If your aim is avoiding duplication in your YAML file, not necessarily including external file, I recommend doing something like this:

development: &default
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: db_dev
  pool: 5
  username: usr
  password: psw
  host: localhost
  port: 3306

dev_cache:
  <<: *default

new:
  <<: *default
  database: db_new

test:
  <<: *default
  database: db_test
猫性小仙女 2024-08-28 09:48:55

如果您只想从另一个 YAML 文件继承,可以通过扩展 ruby​​ YAML 库来提供您所要求的此功能:

https://github.com/entwanderer/yaml_extend

https://rubygems.org/ gems/yaml_extend

用法

yaml_extend 将方法 YAML#ext_load_file 添加到 YAML。

此方法的工作方式类似于原始 YAML#load_file,通过文件继承对其进行扩展。

示例

# start.yml
extends: 'super.yml'
data:
    name: 'Mr. Superman'
    age: 134    
    favorites:
        - 'Raspberrys'

-

# super.yml
data:
    name: 'Unknown'
    power: 2000
    favorites:
        - 'Bananas'
        - 'Apples'

基本继承

YAML.ext_load_file('start.yml')

导致

data:
    name: 'Mr. Superman'
    age: 134
    power: 2000
    favorites:
        - 'Bananas'
        - 'Apples'
        - 'Raspberrys'

If you just want to inherit from another YAML file, there is a gem providing this functionality you are asking for by extending the ruby YAML library:

https://github.com/entwanderer/yaml_extend

https://rubygems.org/gems/yaml_extend

Usage

yaml_extend adds the method YAML#ext_load_file to YAML.

This method works like the original YAML#load_file, by extending it with file inheritance.

Examples

# start.yml
extends: 'super.yml'
data:
    name: 'Mr. Superman'
    age: 134    
    favorites:
        - 'Raspberrys'

-

# super.yml
data:
    name: 'Unknown'
    power: 2000
    favorites:
        - 'Bananas'
        - 'Apples'

Basic Inheritance

YAML.ext_load_file('start.yml')

results in

data:
    name: 'Mr. Superman'
    age: 134
    power: 2000
    favorites:
        - 'Bananas'
        - 'Apples'
        - 'Raspberrys'
旧瑾黎汐 2024-08-28 09:48:55

我正在使用这个:

load_config.rb(初始化程序)

cf_1 = YAML::load(File.read("/etc/my_app/config.yml"))
cf_2 = YAML::load(File.read(File.join(Rails.root, "config", "config.yml")))
CONFIG = cf_1.merge(cf_2)

稍后,您可以通过执行以下操作来访问配置值:

CONFIG['value']

I'm using this:

load_config.rb (initializer)

cf_1 = YAML::load(File.read("/etc/my_app/config.yml"))
cf_2 = YAML::load(File.read(File.join(Rails.root, "config", "config.yml")))
CONFIG = cf_1.merge(cf_2)

Later, you can access config values by doing:

CONFIG['value']
谜兔 2024-08-28 09:48:55
  1. !include 不是指令而是标签。
  2. 它不是 Python(或 PyYAML)的功能,而是“poze”库的功能:

    poze.configuration 公开了一个名为 include 的默认指令。

  3. YAML 规范没有定义这样的标准标签。

  1. !include is not a directive but a tag.
  2. it is not a feature of Python (or PyYAML) but a feature of the "poze" library:

    poze.configuration exposes a default directive named include.

  3. YAML specification does not define such a standard tag.

咿呀咿呀哟 2024-08-28 09:48:55

取决于你需要它做什么。如果需要传输文件,可以对内部yaml文件进行base64编码。

Depends what you need it for. If you need to transport file, you can base64 encode internal yaml file.

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