jimweirich / builder

Provide a simple way to create XML markup and data structures.
http://builder.rubyforge.org
MIT License
361 stars 103 forks source link

Add explicit_nil_handling option to codebase #34

Closed hderms closed 11 years ago

hderms commented 11 years ago

Fixes issue 33 which requests that there be an option to specify how Builder treats nil values from initialization. Without specifying the explicit_nil_handling option as true, the codebase functions the same way.

    b = Builder::XmlMarkup.new 
    b.tag! "foo", "bar"

yields an output of

<foo>bar</foo>

as does

    b = Builder::XmlMarkup.new explicit_nil_handling: false
    b.tag! "foo", "bar"

However, when a tag has a nil value, and the option is turned on, it adds a nil="true" attribute to the tag, in order to match the way Rails explicitly denotes nil values when rendering ActiveRecord objects as xml.

    b = Builder::XmlMarkup.new explicit_nil_handling: true
    b.tag! "foo", nil

yields

<foo nil="true"/>

commit 430b9762c436f180db8686407b5002cb4f71e8b9 Author: Dermot Haughey hderms@gmail.com Date: Tue Jan 15 19:36:00 2013 -0600

Add test

commit b9adb4d916ae1cf897ea2c1f39f5ad0c0def4460 Author: Dermot Haughey hderms@gmail.com Date: Tue Jan 15 19:35:50 2013 -0600

Cleanup of code

commit 8e8a7873b48e956e4642d31265dc767b5fafb096 Author: Dermot Haughey hderms@gmail.com Date: Tue Jan 15 19:25:42 2013 -0600

Make changes to add option
jimweirich commented 11 years ago

I'm not entirely convinced that this is a general purpose change, but I did go ahead and merge it.