Social Media
More About This Website

My name is Wayne Robinson and I'm a web applications developer from Queensland, Australia. In August 2005 I discovered Ruby on Rails and instantly fell in love. From that point forward, Ruby on Rails has been my language of choice for new projects however, I still use PHP to maintain some legacy applications.

Categories
Login
Monday
May012006

Ruby on Rails - ActiveRecord#build_from_xml function

I was playing with the new to_xml feature of Ruby on Rails and I found myself wondering... if you can create XML from ActiveRecord objects, why can't you create ActiveRecord objects from XML?

After searching for a while in the RoR Documentation I wasn't able to find the inverse functionality of to_xml. So now, it seems, I have an opportunity to contribute back to the Rails community with an a functional improvement of my own. I announce to you the build_from_xml method to ActiveRecord.

Just place the below code in your config/environment.rb file.


  require "rexml/document"
  module ActiveRecord
    class Base
      def self.build_from_xml(xml)
        xml = REXML::Document.new(xml) if xml.class == String

        ar = self.new
        xml.elements[1].elements.each do | ele |
          sym = ele.name.underscore.to_sym

          # An association
          if ele.has_elements?
            klass = self.reflect_on_association(sym).klass
            ar.__send__(sym) << klass.build_from_xml(ele)

          # An attribute
          else
            ar[sym] = ele.text
          end
        end

        return ar
      end
    end
  end

You can call this from the main class of any ActiveRecord object. Here is an example.

This ruby code:


  firm_xml = File.new("firm_data.xml").read
  firm = Firm.build_from_xml(firm_xml)

Will convert this XML file into a fully functional ActiveRecord object, including the associations.


  <firm>
    <rating type="integer">1</rating>
    <name>37signals</name>
    <clients>
      <client>
        <rating type="integer">1</rating>
        <name>Summit</name>
        <id type="integer">1</id>
        <firm-id type="integer">1</firm-id>
      </client>
      <client>
        <rating type="integer">1</rating>
        <name>Microsoft</name>
        <id type="integer">2</id>
        <firm-id type="integer">1</firm-id>
      </client>
    </clients>
    <accounts>
      <account>
        <id type="integer">1</id>
        <firm-id type="integer">1</firm-id>
        <credit-limit type="integer">50</credit-limit>
      </account>
    </accounts>
    <id type="integer">1</id>
  </firm>

You may have noticed one caveat. This function accepts well formed XML code only that conforms to your model. If it doesn't, it may produce unpredictable results but will probably raise the usual ActiveRecord exceptions in most non-trivial error cases. Oh, and it requires REXML, but you knew that already right.

I will probably convert this to a plugin in the not-to-distant future. That is if the code isn't included in Rails' release branch (hint, hint).

Thursday
Apr272006

Good vs Great software developers.

I was recently asked an interesting question as to whether I perceive myself to be a great developer. As someone who's previously been responsible for employing technical staff, here are my opinions on the differences between good developers and absolutely great developers

  • Language is unimportant:

    Programming languages are all very much the same and have some common roots.

    A good programmer will learn a number of these languages to allow him to be more flexible for his employers.

    A great programmer will learn and understand the root language and semantics (like Latin for most European languages) and be able to adjust quickly to other languages as his employer requires.

  • Continual self development:

    The IT industry is a fast-paced beast. Ruby on Rails was only released in 2005 yet it has revolutionised the way powerful web-based applications can be developed.

    A good developer will stay abreast of news and technologies that most affect him (usually within the technologies he's already familiar with).

    A great developer will keep informed about as much as possible on as much as possible. Not just IT-related news, but he needs to know what's happening in the rest of the world. A great developer will also continually evaluate and test how these technologies can be used to solve the issues he's faced with on a day-to-day basis. Agility is the great developer's best friend.

  • Problem solving:

    There is so much more to creating applications than writing code. Most problems don't lend themselves to being easily mapped to solutions. Of course there are usually many different solutions to the same problem, some are more correct/elegant/efficient than others.

    A good developer will break a large problem into many parts and solve each part individually following a set of standards/rules.

    A great developer will also do this, but be able to take into account the complete problem-set when deciding on a solutions to the constituent parts. Also, due to the great developers large knowledge base, solutions tend to be more correct/elegant/efficient than those of the good developer.

  • Social/communication skills:

    Developers (especially web-based developers) are ultimately developing applications to be used by people.

    A good developer understands this and has empathy for the end user when developing.

    A great developer will involve the user in his design and development process. He will stay in constant contact with the end-user and attempt to provide as much of the core value required from the application as quickly as possible.

Well, that's just my opinion of course. I'm sure I've missed a number of qualities others may feel are more important than these, or listed some others don't agree with. If you're a great developer, why not drop me a line and we can chat over some a hot pot of ruby code.

Thursday
Apr272006

Rails is truly amazing.

I can never get over how truly amazing Ruby on Rails is. I was recently asked to create a quick and nasty demo application to showcase some of my skills. So, following the provided specs, I whipped up the following application in less than a day (of course, I spent a larger portion of that than I'd like to admit on user interface tweaks - I wish I had design talent).

Now, I'm not a graphic designer/illustrator by any means, but my coding is pretty good. You can check out the application here. It's a pretty basic file uploader with the ability to make files public. Also, the upload supports RTF and HTML in addition to plain text.

Wednesday
Apr262006

A brave new world.

I find it important to stay as informed as possible about the "goings-on" in the world. As a result, I read a lot of blogs and paper-based journals. Finally, I feel I should contribute to this truly global community of information so I've started this blog.

My hopes are this blog will contain helpful articles, personal musings and general highlights of other interesting things happening in the world. Although previous attempts of keeping a diary of any kind have ended in failure, it's about time I made an effort to document the life and times of me, Wayne Robinson, for all the world to see.

Page 1 ... 3 4 5 6 7