ã½ã¼ã¹ã®ä¸ã§å®æ°ã¨ãã¦æ±ãã®ã§ã¯ãªããè¨å®ãã¡ã¤ã«çã«æ±ããã
Rubyã¯ãããªãã«é·ãé使ã£ã¦ãã¾ãããRailsã¯ã¡ã¸ã£ã¼ãã¼ã¸ã§ã³æ¯ã«ã¡ãã£ã¨è§¦ããããã§ãã¾ã£ãããã¹ããã©ã¯ãã£ã¹ã¨ãç¥ããªããã§ããã
ã§ããã¾ã¯ä½ãéã®ãææ¬ã¨ã㦠日本Ruby会議 2010, 8月27日〜29日 ã®ã½ã¼ã¹ã§ãã ruby-no-kai/rubykaigi · GitHub ãè¦ã¦ãã¯ããããªã®ã¯ãã¯ãã¨ããã¹ã¿ã³ã¹ã§ãã£ã¦ã¾ãã
ã§ãæ¬é¡ã®è¨å®ãã¡ã¤ã«ã®æ±ãã«ã¤ãã¦
ä¸è¨ rubykaigi ã® Gemfile ãè¦ã㨠'configatron' ã£ã¦ããã®ãããã
ãªã®ã§ãããã cool ãªè¨å®ãã¡ã¤ã«ãæ±ãã©ã¤ãã©ãªãªã®ããããã¨æã£ã¦ãã®ã§ãããã¡ããã© id:ursm ãããã
@sugamasao æè¿ã¯ Settingslogic ã使ã£ã¦ãã¾ã
2010-09-02 21:35:47 via Brizzly to @sugamasao
ã¨ããå©è¨ãé ããã®ã§ããã£ã¡ã§è©¦ãã¦ã¿ããã¨ã«ãã¾ããã
Settingslogicã®ä½¿ãæ¹
binarylogic/settingslogic · GitHub ã® README ã«ããéãã§ããâ¦â¦
- Gemfile ã« 'settingslogic' ã追å
- bundle update ã§ã¤ã³ã¹ãã¼ã«
è¨å®ãã¡ã¤ã«ç¨ model ãä½ã
vim app/models/settings.rb
class Settings < Settingslogic source "#{Rails.root}/config/application.yml" namespace Rails.env end
ãã ãã°ãã¼ãã«ãªè¨å®ãããããªã namepsace ã¯ç¡ãã¦ãè¯ãï¼è©³ããã¯å¾è¿°ï¼ã
è¨å®ãã¡ã¤ã«ãä½æãã
ä¸è¨ã® source ã§æå®ãã¦ãããã¹ã¨åãããå¿ è¦ãããã®ã§æ³¨æã
vim config/application.yml
# app/config/application.yml defaults: &defaults cool: saweet: nested settings neat_setting: 24 awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %> development: <<: *defaults neat_setting: 800 test: <<: *defaults production: <<: *defaults
ãããããã¨ã§ã Rails ã®ç°å¢å¤æ°ã«ãã£ã¦ defaults + test ãªã©ãå¤åããããã¨ãã§ããã®ã§ãç°å¢ã«ãã£ã¦å¤ãå¤ãããå ´åã§ãç¹ã«ä½è¨ãªæé ãè¸ããã¨ãªããæè»ã«å¯¾å¿ã§ããã
ã¾ãã namespace ã§å¤ãå¤ããå¿
è¦ãªãããä¸è¦ã ããã¨ããå ´å㯠namespace ãã³ã¡ã³ãã¢ã¦ããã¦ã application.yml ã®ã»ã㧠defaults ãªã©ãè¨è¼ããç´æ¥ yml ã§å¤ãæ¸ãã¦ãã¾ãã°ä½¿ããï¼éã«è¨ãã°ãnamespace ãæå®ããå ´åã¯ã対å¿ãã namespace ã®å®ç¾©ãç¡ãã¨ä½ãèªã¿è¾¼ãã§ãããªãï¼ã
èªã¿åºãæ¹
app/controllers/hoge_controller.rb å ããæ®éã«èªã¿åºããã¨ãã§ããã
logger.debug Settings.cool.saweet
#=> nested settings
Settingslogic æ°ã«å ¥ã£ã
model ãæãã¨ããã®ãã¦ãã¼ã¯ã§ããã©ãyml ã§æ¸ããã®ãç¹ã«æèãããã®ã¾ã¾ä½¿ãã¦ã©ã¯ã§è¯ãã§ãããããã¯è¯ãï¼
enjoy!