2013-12-17 1 views
2

configatron을 사용하여 구성 값을 저장하고 있습니다. 범위 값이 클래스의 메서드 내에있는 경우를 제외하고는 문제없이 config 값에 액세스 할 수 있습니다. 루비 여기에 2.0.0구성 단일체? 내 수업에서 configatron에 액세스 할 수없는 이유는 무엇입니까?

내가 configatron 사용하고 3.0.0-RC1 내가 'tc_tron.rb'

require 'configatron' 

class TcTron 
    def simple(url) 
    puts "-------entering simple-------" 
    p url 
    p configatron 
    p configatron.url 
    p configatron.database.server 
    puts "-------finishing simple-------" 
    end 
end 

# setup the configatron. I assume this is a singleton 
configatron.url = "this is a url string" 
configatron.database.server = "this is a database server name" 

# this should print out all the stuff in the configatron 
p configatron 
p configatron.url 
p configatron.database.server 

# create the object and call the simple method. 
a = TcTron.new 
a.simple("called URL") 

# this should print out all the stuff in the configatron 
p configatron 
p configatron.url 
p configatron.database.server 

I라는 하나의 파일에 사용하고 소스입니다 코드를 실행, 나는 사이

{:url=>"this is a url string", :database=>{:server=>"this is a database server name"}} 
"this is a url string" 
"this is a database server name" 
-------entering simple------- 
"called URL" 
{} 
{} 
{} 
-------finishing simple------- 
{:url=>"this is a url string", :database=>{:server=>"this is a database server name"}} 
"this is a url string" 
"this is a database server name" 

을 얻는 '입력 간단한'내가이 configatron 싱글을받지 못했습니다 이유를 모르겠어요 출력 '을 간단하게 마무리'.

무엇이 누락 되었습니까? Kernel마다 객체에 사용 가능한 방법하게 Object에 포함되므로

답변

2

configatron의 현재 구현 here

에서

module Kernel 
    def configatron 
    @__configatron ||= Configatron::Store.new 
    end 
end 

이다. 그러나 메서드는 인스턴스 변수를 설정하기 만하면 해당 인스턴스는 각 인스턴스에서만 사용할 수 있습니다. 모든 직업이 세계적으로 접근 가능한 상점을 제공하고있는 보석에 이상한 선택입니다. 당신은 자신이 대신 원숭이를 피하기 위해 require 'configatron/core'를 사용하여이 문제를 해결할 수처럼 V2.4에서

그들은 아마 here

보이는에서 훨씬 더

module Kernel 
    # Provides access to the Configatron storage system. 
    def configatron 
    Configatron.instance 
    end 
end 

근무 싱글에 액세스하기 위해 비슷한 방법을 사용 - 패치하고 자신 만의 싱글 톤 래퍼를 제공하십시오.

+0

내가 말할 수있는 것은 고맙다. 놀랍다. – John