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

后端 未结 7 1416
悲&欢浪女
悲&欢浪女 2020-12-28 16:13

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 que

7条回答
  •  自闭症患者
    2020-12-28 16:58

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

提交回复
热议问题