DISQUS

Life is grand: A dirty Ruby

  • Farrel Lifson · 3 years ago
    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.
  • Farrel Lifson · 3 years ago
    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
  • Paul Watson · 3 years ago
    Thank you for those two comments Farrel. The last one is especially interesting. I'll give the to_a trick a try.
  • Farrel Lifson · 3 years ago
    I did some further experimenting and it seems it's recommended to use Array(object) instead of object.to_a.
  • Paul Watson · 3 years ago
    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.