2014-04-16 1 views
3

일부 프로젝트 구성의 YAML 파일이 있는데 코드 유지 보수를 간단하게하기 위해 코드 일부를 다시 사용하고자합니다. 그래서 앵커를 사용하여이를 시도하지만 이전 노드를 무시하고 싶습니다. 그게 가능하니? 다음은 YAML 노드 덮어 쓰기

내 예입니다

default: &default 
    output: &default_output 
     make_video: true 
     take_screenshot: true 
    browser: &default_browser 
     type: :chrome 
     width: 1024 
     height: 1280 
    logger: &default_logger 
     level: TRACE 
     output_type: :file 

chrome_browser: &chrome_browser 
    <<: *default 
    browser: 
     type: :chrome 
     user_agent: user_agent_string_for_chrome 

firefox_browser: &firefox_browser 
    <<: *default 
    browser: 
     type: :firefox 
     user_agent: user_agent_string_for_firefox 

좋아, 그래서 이것은 첫 번째 부분입니다 :이 할 수 있습니까? firefox_browser가 "type"을 재정의합니까?

이제 두 번째 부분 :

profile: 
    <<: *default 
    staging: 
     europe: 
      url: www.staging-europe-site.com 
      chrome: 
       <<: *browser_chrome 
      firefox: 
       <<: *browser_firefox 
     america: 
      url: www.staging-america-site.com 
      chrome: 
       <<: &browser_chrome 
      firefox: 
       <<: &browser_firefox      
    live: 
     europe: 
      url: www.europe-site.com 
      chrome: 
       <<: &browser_chrome 
      firefox: 
       <<: &browser_firefox 
     america: 
      url: www.america-site.com 
      chrome: 
       <<: &browser_chrome 
      firefox: 
       <<: &browser_firefox 

내가는 YAML을 읽은 후, 내가 할 수있는 것을 위해 그런 일을 할 수있는 :

profile_yaml['staging.europe.chrome'] 

내가 모든 구성을 얻을?

답변

2

첫 번째 질문에 대해서는 browser (default)의 값이 chrome_browser에 정의 된 것으로 바뀌므로 원하는 것을 수행하지 않습니다. 나무 합병이나 이와 유사한 일은 없습니다. 당신이 다시 아마도 부족 YAML에 덤프 경우

output: 
    make_video: true 
    take_screenshot: true 
logger: 
    level: TRACE 
    output_type: :file 
browser: 
    type: :chrome 
    user_agent: user_agent_string_for_chrome 

:

그래서 두 번째 예를 들어, 거기 당신은 끝낼. 물론 이러한 목표를 달성하는 다른 방법이 있지만 파서가 그렇게하도록 도와야합니다.