LoginSignup
32
21

More than 5 years have passed since last update.

yaml を DRY に書く

Last updated at Posted at 2014-02-06

や、まあ Rails の errors とかやってるので、それみたらいいんですが、一応ね。
たとえば STI してるモデルとかで同じようなの書いたりすると思います。
そんな時は、こんな感じで DRY に書けますね。

修正前

      a_setting:
        name: 名前
        color: 
        hoge: hoge
        fuga: fuga
      b_setting:
        name: 名前
        color: 
        hoge: hoge
        fuga: fuga
      c_setting:
        name: 名前
        color: 
        hoge: hoge
        fuga: fuga

修正後

      base_setting: &base_setting
        name: 名前
        color: 
        hoge: hoge
        fuga: fuga
      a_setting:
        <<: *base_setting
      b_setting:
        <<: *base_setting
      c_setting:
        <<: *base_setting

んんん、いやこれアレ、database.yml で一番使いますよねえ...

  • database.yml の例
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production
32
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
21