On 01/26/2011 09:58 PM, Kamarulnizam Rahim wrote:
> Hi,
> 
> By running the following codes:
> ---------------------------------------------------------------------------
> convert_yaml = YAML::load_file('nizam.yaml')
>     pp
> convert_yaml["System"]["Environmental"]["children"][2]["children"]
>     convert_yaml["System"] = { "children" =>
>          [{"name"=>"nizam",
>            "type"=>"Objective",
>            "subtype"=>"None",
>            "components"=>
>             [{"type"=>"ContentBox",
>               "title"=>"Audit",
>               "args"=>{:content=>"None\n"}},
>              {"type"=>"ChildListingComponent",
>               "title"=>"Current Targets for the Audit Objective:"}]}]}
>     File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)}
> 
> My yaml output turns out to be like this:
> --------------------------------------------------------------------------
> System:
>   children:
>   - name: nizam
>     type: Objective
>     subtype: None
>     components:
>     - type: ContentBox
>       title: Audit
>       args:
>         :content: |
>           None
> 
>     - type: ChildListingComponent
>       title: "Current Targets for the Audit Objective:"
> 
> The actual yaml file before being converted is:
> ---------------------------------------------------------------------------
> System:
>   H&S:
>     name: "Health & Safety"
>     components:
>     - FlashWheel
>     children:
>     - name: "Training and Culture"
>       type: Programme
>       subtype: None
>       components:
>       - type: ContentBox
>         title: "Training and Culture"
>         args:
>           :content: |
>             None
>       - type: ChildListingComponent
>         title: "Training and Culture Programme Objectives"
>     #Children named nizam should be added here
>       children:
>       - name: "Team"
>         type: Objective
>         subtype: None
>         components:
>         - type: ContentBox
>           title: "Team"
>           args:
>             :content: |
>               None
>         - type: ChildListingComponent
>           title: "Current Targets for the Team Objective:"
> 
> My question is how do i add children named 'nizam' under "Health &
> Safety" without jeopardizing other contents (i.e "Training and Culture"
> and further contents of H&S). I think YAML.dump in not the correct way
> to do it. Thanks in advance.

No, YAML.dump is doing what it should.  The problem is that your code
replaces the value of the "System" key entirely with a new hash
containing a single key ("children") whose value is a complex
combination of hashes and arrays.  You effectively clobbered the hash
containing "H&S" key by doing that.

Because the children key appears in multiple levels in your original
data, I can't say for sure where your new entry should go.  You should
first figure out how to get a reference to the structure that should
contain your additional data.  It may be one of the following:

convert_yaml["System"]["H&S"]["children"]
convert_yaml["System"]["H&S"]["children"][0]["children"]

Use pp to display each possibility until you find what you want.  I
think you want the first one.  In either case, the value will be an
array into which you will want to insert your new entry, if I understand
your needs correctly.  Assuming the first possibility I listed, your
modification might go like this:

convert_yaml["System"]["H&S"]["children"] << {
  "name" => "nizam",
  "type" => "Objective",
  "subtype" => "None",
  "components" => [
    {
      "type" => "ContentBox",
      "title" => "Audit",
      "args" => {
        :content => "None\n"
      }
    },
    {
      "type" => "ChildListingComponent",
      "title"=> "Current Targets for the Audit Objective:"
    }
  ]
}

As a word of advice in general, you should try to simplify your tests
until you better understand what you're trying to do.  In this case,
make a much simplified YAML file to start with and play around with
modifying the data in irb or a stand alone test script.  Then, as you
understand how to use your tools with your sample data, gradually add
more complexity to approach that of your actual data.  What you're doing
now is like jumping into the deep end of a pool without really knowing
how to swim. :-)

-Jeremy