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

Added :empty option for existing elements with no content #31

Closed dbarros closed 7 years ago

dbarros commented 13 years ago

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 added an option which handles a situation 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.

My change allows me to do this:

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

Of course, you can combine the two:

xml_accessor :name, :else => "None given", :empty => "An empty string"

The reason why I called it empty is to match the Ruby "".empty? method.

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 using :empty.

dbarros commented 13 years ago

Actually, I've changed my mind on this. Don't pull this request. Back soon with a revised version. Thanks.

lostapathy commented 7 years ago

@dbarros I'm trying to help clean up the backlog and ship an updated version of this gem. Since you don't want this pulled, can you just close it please?