DISQUS

DISQUS Hello! Life is grand is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

A dirty Ruby

Started by paulmwatson · 10 months ago

if soapResponse.getNewTagsSinceResult.respond_to? "tag"
 if soapResponse.getNewTagsSinceResult.tag.is_a? Array
  for tag in soapResponse.getNewTagsSinceResult.tag
   @tag = Tag.find_by_sync_id(tag.id)
  % ... Continue reading »

5 comments

  • I haven't used WSDLDriver but doe the tag object respond to to_a? If so you can do the following:

    if soapResponse.getNewTagsSinceResult.respond_to? "tag"
    for tag in soapResponse.getNewTagsSinceResult.tag.to_a
    @tag = Tag.find_by_sync_id(tag.id)
    if (@tag)
    @tag.name = tag.name
    @tag.save
    else
    @tag = Tag.new
    @tag.name = tag.name
    @tag.sync_id = tag.id
    @tag.save
    end
    end
    end

    Just a quick warning that currently all objects respond to to_a but this will soon be deprecated so for future versions it's better to check that the tag object class explicity implements to_a.
  • Also if you really want to squeeze things down you can do this:

    @tag = Tag.find_by_syn_id(tag.id) || Tag.new
    @tag.name = tag.name
    @tag.sync_id ||= tag.id
    @tag.save
  • Thank you for those two comments Farrel. The last one is especially interesting. I'll give the to_a trick a try.
  • I did some further experimenting and it seems it's recommended to use Array(object) instead of object.to_a.
  • How fantastic. I took the above code and cut it down to just this:
    if soapResponse.getNewTagsSinceResult.respond_to? "tag"
      for tag_current in Array(soapResponse.getNewTagsSinceResult.tag)
          tag = Tag.find_by_sync_id(tag_current.id) || Tag.new
        tag.name = tag_current.name
        tag.sync_id = tag_current.id
        tag.save
      end
    end


    Thanks Farrel.

Add New Comment

Returning? Login