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

时间:2023-01-15 09:13:32

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

在YAML中有自定义标记让ruby在YAML文件中包含YAML文件吗?

#E.g.:  --- !includefilename: 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.

我想知道是否有一些Ruby的自定义标记与Python的类似。

7 个解决方案

#1


15  

If you are in Rails, YAML can include ERB.

如果您在Rails中,YAML可以包含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

database.sqlite.yml

sqlite: &defaults  adapter: sqlite3  pool: 5  timeout: 5000development:  <<: *defaults  database: db/development.sqlite3test:  <<: *defaults  database: db/test.sqlite3production:  <<: *defaults  database: db/production.sqlite3

database.mysql.yml

database.mysql.yml

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

#2


14  

I found a way to address my scenario using ERB.

我找到了一种使用ERB解决方案的方法。

I monkey patched YAML module to add two new methods

我修改了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

I have three YAML files.

我有三个YAML文件。

mod1_config.yml

mod1_config.yml

mod1:    age: 30    city: San Francisco

mod2_config.yml

mod2_config.yml

mod2:    menu: menu1    window: window1

all_config.yml

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.

使用yaml::load_erb来解析yaml文件,而不是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. 不支持文档合并吗
  3. Last include overwrites same named keys
  4. 最后包括覆盖相同的命名键

#3


8  

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

如果你的目标是避免YAML文件中的重复,不一定包括外部文件,我建议这样做:

development: &default  adapter: mysql  encoding: utf8  reconnect: false  database: db_dev  pool: 5  username: usr  password: psw  host: localhost  port: 3306dev_cache:  <<: *defaultnew:  <<: *default  database: db_newtest:  <<: *default  database: db_test

#4


2  

I'm using this:

我用这个:

load_config.rb (initializer)

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)

Later, you can access config values by doing:

稍后,您可以通过以下方式访问配置值:

CONFIG['value']

#5


1  

  1. !include is not a directive but a tag.
  2. !include不是指令,而是标记。
  3. it is not a feature of Python (or PyYAML) but a feature of the "poze" library:

    它不是Python(或PyYAML)的一个特性,而是“poze”库的一个特性:

    poze.configuration exposes a default directive named include.

    poze。配置公开一个名为include的默认指令。

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

    YAML规范没有定义这样的标准标记。

#6


1  

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:

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

https://github.com/entwanderer/yaml_extend

https://github.com/entwanderer/yaml_extend

https://rubygems.org/gems/yaml_extend

https://rubygems.org/gems/yaml_extend

Usage

yaml_extend adds the method YAML#ext_load_file to YAML.

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

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

此方法与原始的YAML#load_file类似,通过使用文件继承扩展它。

Examples

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

-

- - - - - -

# super.ymldata:    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'

#7


0  

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

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

#1


15  

If you are in Rails, YAML can include ERB.

如果您在Rails中,YAML可以包含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

database.sqlite.yml

sqlite: &defaults  adapter: sqlite3  pool: 5  timeout: 5000development:  <<: *defaults  database: db/development.sqlite3test:  <<: *defaults  database: db/test.sqlite3production:  <<: *defaults  database: db/production.sqlite3

database.mysql.yml

database.mysql.yml

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

#2


14  

I found a way to address my scenario using ERB.

我找到了一种使用ERB解决方案的方法。

I monkey patched YAML module to add two new methods

我修改了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

I have three YAML files.

我有三个YAML文件。

mod1_config.yml

mod1_config.yml

mod1:    age: 30    city: San Francisco

mod2_config.yml

mod2_config.yml

mod2:    menu: menu1    window: window1

all_config.yml

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.

使用yaml::load_erb来解析yaml文件,而不是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. 不支持文档合并吗
  3. Last include overwrites same named keys
  4. 最后包括覆盖相同的命名键

#3


8  

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

如果你的目标是避免YAML文件中的重复,不一定包括外部文件,我建议这样做:

development: &default  adapter: mysql  encoding: utf8  reconnect: false  database: db_dev  pool: 5  username: usr  password: psw  host: localhost  port: 3306dev_cache:  <<: *defaultnew:  <<: *default  database: db_newtest:  <<: *default  database: db_test

#4


2  

I'm using this:

我用这个:

load_config.rb (initializer)

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)

Later, you can access config values by doing:

稍后,您可以通过以下方式访问配置值:

CONFIG['value']

#5


1  

  1. !include is not a directive but a tag.
  2. !include不是指令,而是标记。
  3. it is not a feature of Python (or PyYAML) but a feature of the "poze" library:

    它不是Python(或PyYAML)的一个特性,而是“poze”库的一个特性:

    poze.configuration exposes a default directive named include.

    poze。配置公开一个名为include的默认指令。

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

    YAML规范没有定义这样的标准标记。

#6


1  

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:

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

https://github.com/entwanderer/yaml_extend

https://github.com/entwanderer/yaml_extend

https://rubygems.org/gems/yaml_extend

https://rubygems.org/gems/yaml_extend

Usage

yaml_extend adds the method YAML#ext_load_file to YAML.

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

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

此方法与原始的YAML#load_file类似,通过使用文件继承扩展它。

Examples

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

-

- - - - - -

# super.ymldata:    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'

#7


0  

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

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