How to include a YAML file inside a YAML file in Ruby

Viewed 19258

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.

7 Answers

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

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'
  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.

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

Related