Empact / roxml

ROXML is a module for binding Ruby classes to XML. It supports custom mapping and bidirectional marshalling between Ruby and XML using annotation-style class methods, via Nokogiri or LibXML.
http://roxml.rubyforge.org/
MIT License
223 stars 176 forks source link

:else now works when elements exist, but contain no content. #32

Closed dbarros closed 7 years ago

dbarros commented 13 years ago

I had second thoughts on the previous change I made, where I added the :empty option. I've now just placed the same functionality I intended with :empty in to the existing :else option.

I've duplicated my original pull request comments (for :empty) here as reference:

As you know, the :else options is useful when an element is not in the XML at all. For example:

<person>
</person>

xml_accessor :name, :else => "None given"

I've changed the :else option to handle situations where the XML is something like this:

<person>
  <name />
</person>

As you can see, the element is there, it just has no content, which is different to the previous example. In the second example, ROXML would return name as nil.

So, for the above examples, if you have the following:

xml_accessor :name, :else => "None given"

The value for :name will be "None given" in the cases where the element is not in the XML at all, or when it's there, but empty, e.g. ""

I needed this because I had numerous places where I was checking for nil? The XML that I was consuming had many elements with no content. Now I can avoid those nil? checks.

dbarros commented 13 years ago

This pull request replaces my previous one https://github.com/Empact/roxml/pull/31