{"id":3515,"date":"2009-12-30T22:31:08","date_gmt":"2009-12-30T14:31:08","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=3515"},"modified":"2011-04-09T01:56:35","modified_gmt":"2011-04-08T17:56:35","slug":"custom-yaml-config","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/3515-custom-yaml-config","title":{"rendered":"\u81ea\u5b9a YAML Config \u8a2d\u5b9a\u6a94"},"content":{"rendered":"<p>\u5f9e <a href=\"http:\/\/railstips.org\/2009\/11\/10\/app-configuration-so-simple-your-mama-could-use-it\">RailsTips: Config So Simple Your Mama Could Use It<\/a> \u9019\u7bc7\u770b\u5230\u4e00\u500b\u975e\u5e38\u7c21\u55ae\u6f02\u4eae\u7684\u7528\u6cd5\uff0c\u56e0\u70ba\u9084\u883b\u5e38<br \/>\n\u5728 Rails \u88e1\u9762\u7528\u5230\u7684\uff0c\u7279\u6b64\u4e00\u8a18\uff1a<br \/>\n<!--more--><br \/>\n\u53ef\u4ee5\u5c07\u5982\u4e0b\u7684 YAML \u6a94\u653e\u5728 config \u76ee\u9304\u4e0b\uff0c\u9019\u88e1\u7528\u4e86\u4e00\u62db &#038;DEFAULTS \u53ef\u4ee5\u8907\u88fd\u76f8\u540c\u7684\u90e8\u5206\uff1a<\/p>\n<pre>\r\n<code>\r\nDEFAULTS: &DEFAULTS\r\n  email: no-reply@harmonyapp.com\r\n  email_signature: |\r\n    Regards,\r\n    The Harmony Team\r\n\r\ndevelopment:\r\n  domain: harmonyapp.local\r\n  <<: *DEFAULTS\r\n\r\ntest:\r\n  domain: harmonyapp.com\r\n  <<: *DEFAULTS\r\n\r\nproduction:\r\n  domain: harmonyapp.com\r\n  <<: *DEFAULTS\r\n<\/code>\r\n<\/pre>\n<p>\u7136\u5f8c\u5c07\u4ee5\u4e0b\u9019\u500b Module \u6a94\u653e\u5230 lib \u76ee\u9304\u4e0b (\u6216\u662f config\/initializers \u4e0b\u4e5f\u53ef\u4ee5)\uff1a<\/p>\n<pre>\r\n<code>\r\n# \/lib\/harmony.rb\r\nmodule Harmony\r\n  # Allows accessing config variables from harmony.yml like so:\r\n  # Harmony[:domain] => harmonyapp.com\r\n  def self.[](key)\r\n    unless @config\r\n      raw_config = File.read(RAILS_ROOT + \"\/config\/harmony.yml\")\r\n      @config = YAML.load(raw_config)[RAILS_ENV].symbolize_keys\r\n    end\r\n    @config[key]\r\n  end\r\n\r\n  def self.[]=(key, value)\r\n    @config[key.to_sym] = value\r\n  end\r\nend\r\n<\/code>\r\n<\/pre>\n<p>\u5982\u6b64\u4fbf\u53ef\u4ee5\u8b80\u5230 Harmony[:domain] \u4e86\uff0c\u4f9d\u64da\u4e0d\u540c Rails \u74b0\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f9e RailsTips: Config So Simple Your Mama Could Use It \u9019\u7bc7 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/3515-custom-yaml-config\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u81ea\u5b9a YAML Config \u8a2d\u5b9a\u6a94\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53,31],"tags":[],"class_list":["post-3515","post","type-post","status-publish","format-standard","hentry","category-rails","category-ruby","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-UH","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/3515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=3515"}],"version-history":[{"count":15,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/3515\/revisions"}],"predecessor-version":[{"id":5536,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/3515\/revisions\/5536"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=3515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=3515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=3515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}