Adhearsion中的Ruby YAML文件 - 我应该在哪里加载文件?

时间:2011-09-01 18:11:23

标签: ruby-on-rails ruby yaml

此代码全部存在于dialplan.rb

中的inbound_did上下文中
host_config = YAML::load(File.open("config/hosts.yml")).to_hash
sip_hash = host_config["sip_hash"]
hostnames = host_config["hostnames"]

我正在试图弄清楚是否应该在dialplan.rb或其他地方放置YAML :: load。我想在adhearsars开始时只加载一次,但我不知道如何从拨号方案的范围访问该配置变量......

1 个答案:

答案 0 :(得分:2)

如果你想加载它只有一个然后可能是常数对你好吗?

class Dialplan
  HOST_CONFIG = YAML::load(File.open("config/hosts.yml")).to_hash

  def some_method
    sip_hash = HOST_CONFIG["sip_hash"]
    hostnames = HOST_CONFIG["hostnames"]
  end
end

然后如果你想在另一个类中使用它,那么你可以这样做:

class Other
  def other_method
    sip_hash = Dialplan::HOST_CONFIG["sip_hash"]
    hostnames = Dialplan::HOST_CONFIG["hostnames"]
  end
end