Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

know if a tag is empty or not #37

Open
jlecour opened this issue Dec 2, 2011 · 1 comment
Open

know if a tag is empty or not #37

jlecour opened this issue Dec 2, 2011 · 1 comment

Comments

@jlecour
Copy link

jlecour commented Dec 2, 2011

Let's say I have an XML snippet like this :

<article>
  <title>My Title</title>
  <author/>
</article>

and this mapping :

class Author
  include ROXML

  xml_reader :whatever
end

class Article
  include ROXML

  xml_reader :title
  xml_reader :author, :as => Author
end

Is it possible to know that the author is present without looking inside?
I wish I could something like this

article = Article.from_xml(xml)
puts article.author.whatever if article.author.present?

Thanks for this really great gem. I use it a lot, and am very happy with it.

@lostapathy
Copy link
Collaborator

It appears this works with current versions of the gem. This script outputs what you'd expect:

require 'rubygems'
require 'roxml'

class Author
  include ROXML

  xml_reader :whatever
end

class Article
  include ROXML

  xml_reader :title
  xml_reader :author, :as => Author
end

xml = "<article>  <title>My Title</title><author/></article>"
xml2 = "<article>  <title>My Title</title></article>"

article = Article.from_xml(xml)
puts "present" if article.author.present?

article2 = Article.from_xml(xml2)
puts "not present" unless article2.author.present?

If I'm missing something there please let me know. Otherwise (or if you've long since moved on) I think we can probably close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants