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.xml')
config['mod1']['age'] # 30
config['mod2']['menu'] # menu1
Caveats:
- Does not support document merge.
- The include directive has to be at the top of the file.