Entries from February 1, 2007 - March 1, 2007
ActiveRecord and programmatically working with attributes.
Ruby on Rails is great. Partially because of all the ORM stuff and partially because Ruby is infitely extendable and overrideable (yes, I just made that word up).
On my current project, I have a lot of extended attributes in models that modify other attributes or even other models entirely. So, when I create new objects and assign their attributes, I would prefer the mass assignments to go through these custom mutators.
Now in Ruby, this is relatively simple. When you want to call a method within an object programmatically, you can use the #send method:
str = "Hello world!"
puts str.send(:size)
Displays: 12
However, when you are performing property setting, you have to do some manipulation to the attribute name first before you can assign a value to it. For example, the code for sending the values of a hash to the corresponding mutator methods in a Person object would be the following:
hash = {
:first_name => "Wayne",
:last_name => "Robinson",
:date_of_birth => "1982-02-15"
}
hash.each do | key, value |
person.send("#{key}=", value)
endAs you can see, there is a little bit of repetition there if you have to use that in more than one place. So, I've DRYed this code into the following ActiveRecord extension. Just pop this active_record_getters_and_setters.rb file in your lib/ directory and require it in your environment.rb file with:
require 'active_record_getters_and_setters.rb'
You will now have three extra methods accessible to you in all your ActiveRecord objects.
ActiveRecord::Base#set(attribute, value)
Assigns value to the mutator (setter) for attribute.
ActiveRecord::Base#get(attribute)
Gets the value from the accessor (getter) for attribute.
ActiveRecord::Base#set_attributes(attribute_hash)
Does an ActiveRecord::Base#set for each key/value pair in attribute_hash.
Add all non-versioned controlled files to the subversion repository
Just wrote a small one-liner to add all non-version controlled files in the current path and down to a subversion repository without any unecessary warning messages or errors.
svn st | \
grep -E "^\?" | \
sed -e s/^\?\\s*//g | \
sed -e s/\\s/\\\\\ /g | \
xargs -I{} svn add {}
I have only tested this on Cygwin so far, but it should work fine on Linux and Mac OS X.
TIBCO General Interface
With all the work I've been doing with Ruby on Rails recently, I haven't given much thought to the advancements being made for web-based application development. However, yesterday I stumbled across TIBCO's General Interface.
General Interface is an AJAX GUI design framework, with it's IDE also published as a web application. It communicates with the N-tier via XML-based web services and has a very good selection of well written controls (lists, matrices, charts, etc) for embedding in your application.
I've played a little with General Interface and I'm generally blown-away by it's power and ease of use. The only two things stopping me from jumping in and doing some serious development, is the fact that it is only a GUI interaction tool and not a complete development stack. I would still need to write the N-tier somewhere (yes, I know I could publish Ruby on Rails applications as web-services only).
If you're developing web-based content for an existing application (say, Salesforce or NetSuite) and are looking for an easy to use, powerful, web-based GUI framework. I would suggest evaluating TIBCO's General Interface. Or at leaset having a look at some of their screencasts.
