Generate yaml node bindings and links programmatically

I need to generate this yaml programmatically in Rails:

foo: &foo
  x: 1
  y: 2

bar:
  <<: *foo
  z: 3

which during analysis should have this hash:

output = {
    :foo => {
        :x => 1,
        :y => 2
    },
    :bar => {
        :x => 1,
        :y => 2,
        :z => 3
    }
}

Obviously output.to_yamlgiving an extended syntax. Is there any way to output yml syntax with anchor and nodes programmatically.

+3
source share
1 answer

I had a similar function when I read a lot of YAML files from the initializer and added to the global variable. it is something like this:

APP = Hash.new
Dir.glob("#{Rails.root}/config/data/*.yml").each do |file|
  fdata = File.open(file).read
  APP.merge!(YAML::load(ERB.new(fdata).result(binding)).symbolize_keys!)
end

I have to learn more about your problem in order to write a more specific solution.

- YAML:: load, yaml . ERB , Yaml erb, . . symbolize_keys! [: ], [ "" ] .

0

All Articles