我想使用 Figaro 做配置信息,在 application.yml 中写的配置如下:
EMAIL:
SMTP:
ADDRESS: <<Address>>
PORT: <<Port>>
DOMAIN: <<Domain>>
AUTHENTICATION: <<Auth>>
USER_NAME: <<User Name>>
PASSWORD: <<Password>>
ENABLE_STARTTLS_AUTO: <<Starttls Auto>>
DEFAULT_FROM_EMAIL: <<Default Email>>
DEFAULT_URL: <<Default Url>>
在 Console 下,如果引用``,没有问题:
2.0.0p353 :004 > Figaro.env.EMAIL
=> "{\"SMTP\"=>{\"ADDRESS\"=>\"<<Address>>\", \"PORT\"=><<Port>>, \"DOMAIN\"=>\"<<Domain>>\", \"AUTHENTICATION\"=>\"<<Auth>>\", \"USER_NAME\"=>\"<<User Name>>\", \"PASSWORD\"=><<Password>>, \"ENABLE_STARTTLS_AUTO\"=><<Starttls Auto>>}, \"DEFAULT_FROM_EMAIL\"=>\"<<Default Email>>\", \"DEFAULT_URL\"=>\"<<Default Url>>\"}"
如果引用Figaro.env.EMAIL.SMTP
,则报错:
2.0.0p353 :005 > Figaro.env.EMAIL.SMTP
NoMethodError: undefined method `SMTP' for #<String:0x007fa1280ef940>
from (irb):5
from /Users/diguage/.rvm/gems/ruby-2.0.0-p353@rails402/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/diguage/.rvm/gems/ruby-2.0.0-p353@rails402/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/diguage/.rvm/gems/ruby-2.0.0-p353@rails402/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
如果引用Figaro.env.EMAIL.SMTP.ADDRESS
,也会报上面一样的错误。
Figaro 不支持这种层次配置以及吗?还是我的引用方式不对?(我看网上的一些例子没见过这种层次配置配置的)
注:在群里询问这个问题时,一个朋友说,Figaro 的引用方式是ENV["xxx"]
。我试了一下,两种都可以。