Rails:dotenv嵌套哈希

时间:2016-09-30 15:08:27

标签: ruby-on-rails

我已在我的.env文件中声明了此变量

WARRANTY_DETAILS_PER_MAKE = {"dacia" => {duration: 3, mileage: 100000}, "honda" => {duration: 3, mileage: 100000}, "infiniti" => {duration: 3, mileage: 100000}, "jaguar" => {duration: 3, mileage: 0}, "land-rover"=> {duration: 3, mileage: 100000}, "lexus" => {duration: 3, mileage: 100000}, "mazda" => {duration: 3, mileage: 100000}, "mitsubishi" => {duration: 3, mileage: 100000}, "nissan" => {duration: 3, mileage: 100000}, "subaru" => {duration: 3, mileage: 100000}, "suzuki" => {duration: 3, mileage: 100000}, "toyota" => {duration: 3, mileage: 100000}}
dotenv似乎并不欣赏这一点。

我在运行服务器时得到这个。

/Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/psych.rb:370:in `parse': (<unknown>): did not find expected ',' or '}' while parsing a flow mapping at line 25 column 30 (Psych::SyntaxError)
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/psych.rb:370:in `parse_stream'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/psych.rb:318:in `parse'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/psych.rb:245:in `load'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/application.rb:391:in `secrets'
    from /Users/mike/Documents/Projects/paycar/entre-nous/config/initializers/mail.rb:4:in `<top (required)>'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/engine.rb:652:in `block in load_config_initializer'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/activesupport-4.2.1/lib/active_support/notifications.rb:166:in `instrument'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/engine.rb:651:in `load_config_initializer'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/engine.rb:615:in `each'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/engine.rb:615:in `block in <class:Engine>'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/initializable.rb:30:in `run'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:418:in `block (2 levels) in each_strongly_connected_component_from'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:417:in `block in each_strongly_connected_component_from'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/initializable.rb:44:in `each'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/initializable.rb:44:in `tsort_each_child'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:411:in `call'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:411:in `each_strongly_connected_component_from'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/railties-4.2.1/lib/rails/application.rb:352:in `initialize!'
    from /Users/mike/Documents/Projects/paycar/entre-nous/config/environment.rb:5:in `<top (required)>'
    from /Users/mike/Documents/Projects/paycar/entre-nous/spec/rails_helper.rb:4:in `require'
    from /Users/mike/Documents/Projects/paycar/entre-nous/spec/rails_helper.rb:4:in `<top (required)>'
    from /Users/mike/Documents/Projects/paycar/entre-nous/spec/models/vehicle_spec.rb:23:in `require'
    from /Users/mike/Documents/Projects/paycar/entre-nous/spec/models/vehicle_spec.rb:23:in `<top (required)>'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run'
    from /Users/mike/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/mike/.rvm/gems/ruby-2.1.5@global/gems/rspec-core-3.2.2/exe/rspec:4:in `<top (required)>'
    from /Users/mike/.rvm/gems/ruby-2.1.5@paycar-api/bin/rspec:23:in `load'
    from /Users/mike/.rvm/gems/ruby-2.1.5@paycar-api/bin/rspec:23:in `<main>'
    from /Users/mike/.rvm/gems/ruby-2.1.5@paycar-api/bin/ruby_executable_hooks:15:in `eval'
    from /Users/mike/.rvm/gems/ruby-2.1.5@paycar-api/bin/ruby_executable_hooks:15:in `<main>'

如果我将嵌套哈希值更改为字符串以进行调试,那么一切都会顺利运行。

是我的嵌套哈希的声明不正确还是dotenv的问题?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

DotEnv does not parse除了简单的引用或未引用的值之外的任何其他内容,这与大多数shell处理ENV变量的方式一致。

LINE = /
  \A
  (?:export\s+)?    # optional export
  ([\w\.]+)         # key
  (?:\s*=\s*|:\s+?) # separator
  (                 # optional value begin
    '(?:\'|[^'])*'  #   single quoted value
    |               #   or
    "(?:\"|[^"])*"  #   double quoted value
    |               #   or
    [^#\n]+         #   unquoted value
  )?                # value end
  (?:\s*\#.*)?      # optional comment
  \z
/x

如果你真的不得不使用Ruby哈希,你可以引用它并在其上调用eval:

# .env
FOO = '{ a: "b" }'

# call it:
foo = eval(ENV["FOO"])

或者使用JSON:

# .env
FOO = '{ "a": "b" }'

require 'json'
begin 
  JSON.parse(ENV["FOO"])
rescue JSON::ParserError => e
  logger.error('Failed to parse ENV[FOO].')
  logger.error(e.msg)
end

但看起来你可能正在使用错误的工具来完成工作,而YML配置文件可能更合适。