Rails ã® config ã§è¨å®ã§ãã Rails åã gem ãä½ã
Rails ã¢ããªãéçºãã¦ããã¨ï¼ãã©ã°ã¤ã³ããã¨ã³ã¸ã³ããã§ä½ãããã㨠gem ãä½ã£ã¦ãããã¨ã«ãªãã¾ãï¼ãã®ããã«ãã¦ä½ã£ã gem ã¯å½ç¶ Rails åããªã®ã§ï¼Rails ã¢ããªã±ã¼ã·ã§ã³ã® config ã§è¨å®ã§ããããã«ãããã¨ããã§ãï¼ãã¨ãã°ï¼passingloop ã¨ãã gem ãä½ã£ãã¨ãã¦ï¼ãã® gem 㧠token 㨠email ãè¨å®ã§ããã¨ããã°ï¼
Depot::Application.configure do config.passingloop.token = '15x3oegi' config.passingloop.email = '[email protected]' end
ã®ããã«æ¸ããããã«ãããã¨ããã§ãï¼ãããï¼ãã®ããã«æ¸ãã ãã§ã¯ï¼
[...]/railties-3.1.1/lib/rails/railtie/configuration.rb:78:in `method_missing': undefined method `passingloop' for #<Rails::Application::Configuration:0x000001017db250> (NoMethodError)
ã¨ï¼config.passingloop ãè¦ã¤ãããã«ã¨ã©ã¼ã«ãªãã¾ãï¼ããã§ï¼æ³£ãæ³£ã
Depot::Application.configure do config.passingloop_token = '15x3oegi' config.passingloop_email = '[email protected]' end
㨠"." ã "_" ã«æ¸ãç´ãã¦éããããªãã®ã§ããï¼æ ¼å¥½æªããã¨ãã®ä¸ããã¾ããï¼ããã§ï¼æ¬ã¨ã³ããªã§ã¯ï¼gem ãã¨ã« config ãåå空éãã¤ãããããã«ï¼ããªãã¡ï¼ã¯ããã«ç´¹ä»ãã
Depot::Application.configure do config.passingloop.token = '15x3oegi' config.passingloop.email = '[email protected]' end
ã®ããã«è¨å®ã§ãã gem ã®ä½ãæ¹ã解説ãã¾ãï¼
çã¯ç°¡å Rails::Railtie 㨠ActiveSupport::OrderedOptions
å ã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ãè¦ãã°åããããã«ï¼
undefined method `passingloop' for #<Rails::Application::Configuration:0x000001017db250> (NoMethodError)
ã¨ï¼Rails::Application::Configuration ãªãªãã¸ã§ã¯ãã« passingloop ãç¡ããã¨ãåé¡ãªã®ã§è¿½å ãã¦ããã¾ãï¼ããã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãã Rails::Application::Configuration ãªãªãã¸ã§ã¯ãã®æ£ä½ã¯ï¼Rails::Railtie ã® config ãªã®ã§ï¼gem 㧠config ã«ãã¼ passingloop ã追å ãã¦ããã¾ãï¼config.passingloop ã«è¿½å ãããªãã¸ã§ã¯ãã« Hash ã使ã£ã¦ãããã®ã§ããï¼
ããã«ãããã®ã§ï¼ActiveSupport::OrderedOptions ã使ãã¾ãï¼ãã® ActiveSupport::OrderedOptions ã¯ï¼Hash ã®ããã«ä½¿ãã¦ï¼
hash['name'] = 'value' hash['name'] #=> 'value'
ã¨æ¸ã代ããã«ï¼
ordered_hash.name = 'value' ordered_hash.name #=> 'value'
ã¨æ¸ããåªããã®ã§ãï¼æ©éï¼gem ã« lib/passingloop/railtie.rb ãä½æãï¼
module Passingloop class Railtie < Rails::Railtie config.passingloop = ActiveSupport::OrderedOptions.new end end
lib/passingloop.rb ã§å¿ããªãããã« require ãã¦ï¼
require "passingloop/version" require "passingloop/railtie" module Passingloop ...
rails c ããã¨ï¼
~/work/depot % rails c Loading development environment (Rails 3.1.1) irb(main):001:0> Depot::Application.configure do irb(main):002:1* puts config.passingloop.email irb(main):003:1> puts config.passingloop.token irb(main):004:1> end [email protected] 15x3oegi => nil
ä»åº¦ã¯ãã¾ãããã¾ããï¼
Engine 㨠Plugin ãªããã£ã¨ç°¡å
Rails ã®ã¨ã³ã¸ã³ã¨ãã©ã°ã¤ã³ã¯ï¼å®ã¯ï¼ä¸¡æ¹ã¨ã Railtie ã§ãï¼ã§ãã®ã§ï¼ãã¨ãã°ï¼Rails ã¨ã³ã¸ã³ã config ã§è¨å®å¯è½ã«ããã«ã¯ï¼Railtie ã¯ã©ã¹ãããããä½ãå¿ è¦ã¯ãªãï¼Engine ã¯ã©ã¹ã®ä¸ã§ config ã« ActiveSupport::OrderedOptions ãè¨å®ããã ã㧠ok ã§ãï¼
module Blog class Engine < Rails::Engine isolate_namespace Blog config.blog = ActiveSupport::OrderedOptions.new end end
ããã¯ï¼Blog ã¨ã³ã¸ã³ã®è¨å®ã Rails ã¢ããªã±ã¼ã·ã§ã³ããã§ããããã«ããä¾ã§ãï¼
Railtie ã使ãã°ï¼config 以å¤ã«ã Rails çã«è¨å®ã§ããï¼
æ¬ã¨ã³ããªã§ã¯ï¼gem ç¬èªã®è¨å®é ç®ã Rails ã® config ããè¨å®ã§ããããã«ããæ¹æ³ã説æãã¾ããï¼Railtie ã使ãã°ï¼config 以å¤ã«ã initializer ã generetor, middleware, Rake ã¿ã¹ã¯ãªã©ã gem ããè¨å®ã§ããããã«ãªãã¾ãï¼Gem ã使ããªã Rails éçºã§ã¯ãã¾ãæ°ã«ãããªã Rails::Railtie ã§ããï¼ä¸æ©é²ã㧠gem ä¸å¿ã®éçºãç®æãã®ãªãï¼ä¸åº¦ã½ã¼ã¹ãèªãã§ã¿ããã¨ããå§ããã¾ãï¼