Saturday, April 29, 2006

Database access

Our next objective was to introduce database interaction. We decided to have the following classes for it.
TabularTo show multiple records in a grid
FreeFormTo show one record with multiple fields
DataStoreTo interact with database

In addition two classes were used internally.
  • RecordSet - to hold a retrieved record
  • Column - to hold information about columns in a tabular and rows in freeform
The action class was also extended to support a call to a server side function created as a part of the page definition. To both Tabular and FreeForm, we added some Ajax like features so that:
  • A HTTP XML request can be send from the send from a client action, attached to a tabular column, which will invoke a server side function written in the page.
  • The server side function can create a class, like FreeForm, and sends it to the client
  • The FreeForm is repainted in the client
So there is a tabular portion in the screen which displays name of all the participants. There is also a FreeForm portion which displays more detail. On click on any name the data is fetched from the server side and the FreeForm is updated.

We created a database of all people attending the BarCamp at Chennai to get the page working.

The code looks like this:

module PageTabular

def showFreeform(pNameClicked)
    datastore = DataStore.new("tsgpdc2k", "ubuildmya", "sets")
    datastore.databasename = "SessionDB"

    freeform = Freeform.new
    freeform.name = "DynamicDisplay"

    style = Style.new
    style[:font_size] = 12
    style[:color] = "blue"

    liveNewsText = Text.new("")
    liveNewsText.style = style
    newsDetail = Text.new("")
    newsDetail.style = style
    presentation = Text.new("")
    presentation.style = style
 
    freeform.add("Name", "Attendee Name", liveNewsText)
    freeform.add("Details", "Attendee Details", newsDetail)
    freeform.add("Presentation", "Presentation Given", presentation)

    datastore.sql  = "select * from Attendees where name= ?"
    datastore.setParameter(1, pNameClicked)
    freeform.columns = datastore.select

    return freeform
end

def pageText
    page = Page.new(:PageTabular)

    datastore = DataStore.new("tsgpdc2k","ubuildmya","sets")
    datastore.databasename = "SessionDB"
    datastore.sql  = "select * from Attendees"

    freeformstyle = Style.new
    freeformstyle[:left] = 300
    freeformstyle[:background_color] = '#A5c3ef'

    freeform = Freeform.new
    freeform.name = "DynamicDisplay"
    freeform.width = 350
    freeform.style = freeformstyle
    freeform.valign = :top

    tabularsection1 = Tabular.new
    tabularsection1.maxRow = 100
    tabularsection1.height = 100

    style = Style.new
    style[;color] = "blue"

    name = CheckBox.new('')
    actionclass = Action.new
    actionclass.add(page, 'showFreeform', [name], [freeform])
    actionclass.add(freeform, "refresh")
    name.event[:Click] = actionclass
    name.style = style

    tabularsection1.add("Name", "Attendee Name", name)
    tabularsection1.columns = datastore.select

    page << [tabularsection1, freeform]

    return page
end
end

Thursday, April 13, 2006

Demo a Bar Camp Chennai

A BarCamp was scheduled at Chennai on the 8th and 9th of April. What better place to present rich ruby? So, we decided to go ahead.

To make the presentation more attractive we decided to quickly add a few more features. We still had about 2 weeks time. We wanted to demonstrate the following capabilities.

  • Presentation style
  • Build mash-up with Google map
  • Event programming to invoke either client side action
Before adding presentation style we added Image, Input & Checkbox. It works in the same way as the Text

ListBox, DropDownListBox and OptionButton are yet to be done.

Next step was to assign height and width property for all the classes. It was added for all the classes except for page. Also to provide the developer with display style we created a class for Style. Using the Style class, all the properties available in css can be set.

To get into the world of mash-up we started with Google map. On one hand it is very easy to use and quiet nice to demonstrate. On the other hand it is very powerful and extensive. So we added class for GoogleMap.

The minimum requirement to make this into a mash-up we need to show a place in the map based on user action. To achieve this we need an Action class and need to attach to a client side Event. Accordingly we created an Action class and an Event class. This allows us to click on the city and the city gets shown in the map.

Since Webrick is the web server of choice for Ruby developer, we tried the same code in Webrick and it worked fine.

Need to compare this concept with tag library and web form in .net and see if RR is better? What are the advantages and disadvantages?

Wednesday, April 12, 2006

Start of Rich Ruby

Till two months back I used to believe that programming languages has reached a level of maturity and there is very little scope of innovation. In future, the innovation will come from Model Based Approach. About two months back I had a serious look at Ruby and had to revise my opinion about programming languages.

What I liked about Ruby can be quickly summarized into the following points:

  • Readability of program and simplicity of syntax
  • Everything is an object and every object is extendable
  • Every object has an extensive list of build in functions
  • Operators can be used as function name
  • Meta programming ability of Ruby
With these initial thought I started my hands on with Ruby. The Meta programming ability of Ruby was the biggest attraction and I wanted to see if I could build a level of abstraction for web application.

Personally, I have never liked programming for web. You have to write code at different places using different syntax. For rendering the page you use Html & DHtml, for client side action you use JavaScript, for some visual properties embed in html, for full range of visual properties use Style Sheet. On top of that you use some other language like Java, VB/C# or Php/Perl/Python for server side coding.

This problem made more complex with the introduction of AJAX. Coding required for implementing AJAX interface can become quiet complex. Consequently, the code is also very difficult to maintain.

We got eRuby working under Apache in a Windows machine. I had Why's (poignant) Guide to Ruby and Help which is part of Ruby(fxri - Instant Ruby Enlightenment) to understand the syntax and started off programming on Ruby.

So, the Hello World program looked as follows:

A start.rhtml page was written which included this module and called the show function.

But this was a trivial example and did not prove anything. So, I tried to render a tree view control. I had to write three classes, TreeView, TreeViewFolder and TreeViewNode. It worked beautifully. Using these classes I could easily create a page with multi-level tree.

That looks good. Next step was to try out a cascading menu. Similar to tree view I created Menu, SubMenu and MenuItem and could easily create a page with multi-level cascading menu.

Now I wanted to add controls on the same page and arrange them either horizontally or vertically. To do that I created two classes Row and Column. Both of them will act as a container and arrange the children in a row or in a column. They could be nested, that is a row could contain a column and a column could contain a row. To simply the syntax I used an array and internally converted to row or column.

So, initial experimentation with seem to have achieved a neat level of abstraction! The question was how far we can expand this concept. At this stage I did not see any limit and had a long list of possibilities in my mind.

The next logical step was to explore the existing open source project and see if a similar idea is being pursued. I had a brief look at Ruby on Rails and Nitro. Neither of these projects seems to be focusing on this aspect of web application. So, I decided to start an open source project to take this idea forward.

There were two obvious choice of hosting. SourceForge is large and most popular. However, the community is more Java focused. Most of the top ruby projects in SourceForge are to build editor for ruby using some other language.

On the other hand RubyForge is much smaller and the main focus is on ruby. Almost all the popular ruby open source projects are hosted there.

So, RubyForge was the choice!

For the name the initial thought was to have Ruby Ring or The Ring of Ruby. But finally I settled for RichRuby. It is the short form of rich internet application using Ruby. It feels more appropriate.

Quickly drafted a few colleagues into the project.

So the home page of the project is http://richruby.rubyforge.org

Wanted to make the thought process behind how RichRuby is evolving available to all RR users. So I decided to start this blog.

We had had not yet tested the code on Webrick. Need to try it quickly.