Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 869
Harish Shetty Avatar asked Feb 17 '10 04:02

Harish Shetty


People also ask

What is inside YAML file?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it's a strict superset, it can do everything that JSON can and more.


1 Answers

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 ...
like image 193
Mark Piper Avatar answered Oct 19 '22 09:10

Mark Piper