Feb 5

Strict typing is good mkay

Posted by Cliff

I’m currently in the final stages of a refactor and I thought I’d just share this little tidbit on why strict typing is a good thing in AS3, especially when working in an agile environment where things can change quickly as requirements unfold and mutate.

Let’s say you’re working with data via AMF, and you have a Project value object with the properties ‘id’, ‘name’ and ’status’.

public class Project {
  public var id:Number;
  public var name:String;
  public var status:String;
}

In no time at all the rest of your code will be littered with references to these properties, and obviously if any of these properties changes during refactoring will be instantly picked up by the compiler. No biggie refactoring those. But there are instances where changes can slip under the radar.

For example (examples are always good), take a look at this snippet:

private function on_result(event:ResultEvent):void {
  this.status = event.result.status;
}

See the subtlety? In this instance, event.result is an Object, which is dynamically typed. Therefore accessing erroneous properties will not be picked up by the compiler, and this ultimately leads to subtle, frustrating bugs and loss of hair.

To avoid this, ensure you type cast dynamic objects to their appropriate types before accessing properties thusly:

private function on_result(event:ResultEvent):void {
  this.status = (event.result as Project).status;
}

Now if the status property is renamed during refactoring, the compiler will instantly pick it up and the world will seem a little brighter.

Feb 3

I’ve been running with the Rails MySQL adapter for a while now after failing to build the native MySQL gem under Leopard. The problem, I thought, was that I’m using the Macports MySQL, and it turns out I was right. If you attempt to build the MySQL gem and you get an error like this:

Building native extensions.  This could take a while...
ERROR:  While executing gem ...
(Gem::Installer::ExtensionBuildError)
    ERROR: Failed to build gem native extension.

ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no

Gem files will remain installed in
/opt/local/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
Results logged to /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/gem_make.out

Then you’ll need to add a couple of things to your Gem command line to get it building:

If you’re just installing or updating the gem, try this:

sudo gem install mysql -- --with-mysql=/opt/local/lib/mysql5 --with-mysql-lib=/opt/local/lib/mysql5/mysql --with-mysql-include=/opt/local/include/mysql5/mysql

If you’re updating all your gems (this one caught me out), then try this:

sudo gem update -- --with-mysql=/opt/local/lib/mysql5 --with-mysql-lib=/opt/local/lib/mysql5/mysql --with-mysql-include=/opt/local/include/mysql5/mysql
Jan 26

AppFresh Shmapfresh..

Posted by Cliff

Well, AppFresh is a great idea but has many, many issues. Not least that the moderator won’t moderate my criticisms. Free speech anyone? Don’t think I’ll bother with this one..

Edit: Why the outburst? It’s just one of those things that bugs me. It’s like saying something to someone’s face and having them completely blank you. And really, it’s a piece of software not a secret government institution - is moderation really necessary beyond curbing spam?

Jan 13

Oops, sorry!

Posted by Cliff

Just a quick apology to anyone who has my blog in their RSS reader. I’ve just switched from Mephisto back to WordPress and in the process of converting all my posts and comments (there is no automated conversion routine, I’ve had to do this by hand; with a little help from Ruby) I’ve made a few mistakes, messed up a few articles, and as a result I’ve been making quite a few edits. So apologies for the noise, please remain seated while we ride out this turbulence.

Why the switch? Well actually a few reasons, but primarily because Mephisto seems to be at a standstill, and besides being a sucker for new features and plugins; I don’t believe that Mephisto can be 100% complete and secure after such a relatively short development period. I haven’t seen a single public update since I installed it (of course I could use -trunk.. at risk of watching my blog fall over every time a breaking change is committed..). That’s maybe 8+ months without a public release. So whether you agree with my reasons or not (I got flamed on #rubyonrails a couple of times for mentioning I may switch..), there it is. I’ve switched. So stick that in your console and compile it :-P

Dec 12

Note: read the notes at the end of this article first!

Ok, so we’re using RubyAMF trunk on a brand spanking new project, and all in all it’s pretty darn cool. I love AMF anwyay, irrespective of the crimes against standards, it’s efficient - and I like efficient. But I do care about standards, and for that reason I can’t bear the thought of producing a service that can only talk AMF, but I don’t want to go through extra hoops to achieve it.That’s one of the nice things about Edge (Oops, just remembered it’s now a release) Rails. Using respond_to, a controller can dynamically serve content based on the client. Meaning this is possible:

def ProjectsController
  def create
    @project = Project.create_with_company!(
      params[:project],
      params[:company]
    )
    respond_to do |format|
      format.xml do { render :xml => @project.to_xml }
      format.html do { render :action => "some_action" }
      # etc..
    end
  rescue
    # Blah blah
  end
end

This is nice. It means that the only time we need to care about what the client is, is when we need to send something back. As long as we receive a standard params hash.I may well be wrong, but there doesn’t appear to be a way to configure RubyAMF to map an incoming Value Object straight to a hash. Instead RubyAMF wants to instantiate the appropriate ActiveRecord model for you, which means that the controller will ultimately need to behave differently when an AMF client makes a request. This is no good.So, after much experimentation with ParameterMappings, ValueObjects mappings and RubyAMF configuration settings, I came up with this little gem (why are these things never obvious until you’ve been around the houses a few times?). To map your incoming Value Objects directly to the params hash, declare your ParameterMappings like this example:

ParameterMappings.register(
  :controller => :ProjectsController,
  :action     => :create,
  :params     => {
    :company => "[0].attributes",
    :project => "[1].attributes"
  }
)

Notice the call to .attributes. Now this controller will receive a hash containing two other hashes, one containing the company attributes and the other containing the project attributes; in other words, a regular params hash. Our controller can now simply return the results as AMF the same as any other request:

def ProjectsController
  def create
    @project = Project.create_with_company!(
      params[:project],
      params[:company]
    )
    respond_to do |format|
      format.xml do { render :xml => @project.to_xml }
      format.html do { render :action => "some_action" }
      format.amf do { render :amf => @project }
      # etc..
    end
  rescue
    # Blah blah
  end
end

It’s a bit of a hack. RubyAMF spends pointless time and effort gift wrapping an ActiveRecord model for us and we tear it to shreds and throw it away. But hey, it works, and us kids still get our toys.

Update: It appears I may have been hasty in my assumptions. Setting ParameterMappings.scaffolding to true will achieve the same effect. I know I’ve tried this, and I had an issue with it, I just don’t remember what it was. In fact I think I’ve possibly tried every single possible permutation of configuration settings..

Update: Indeed ParameterMappings.scaffolding does achieve the same effect, so this whole article is actually redundant. Ho hum :-)

Nov 13

Radiohead fan artwork

Posted by Cliff

This is great.. A bunch of Radiohead fans have been busy creating their own custom artwork for the In Rainbows album (which is awesome incidentally). Some of the submissions are brilliant.. See them here.

Nov 12

TextMate Rails Cheat Sheet

Posted by Cliff

This is actually a duplication of this, but when I already have a million tabs open during the course of development, I’d rather not open another with a big fat Flash applet in it. So I created myself the following text only version. The characters I’ve used aren’t perfect, I’ll fix them when I get a chance..

Legend:

Apple / Command
Shift
Option
Control
Tab
Enter

File navigation

Go to file ⌘T
Go to line ⌘L
Go to method ⇑⌘T
Go to file ⇑⌘F
Go to file ∧⌘R

Editing

Code completion Escape
Fold / Unfold F1
Comment selection ⌘/
Indent / Unindent selection ⌘[ and ⌘]
Move selection ∧⌘(arrow)
Edit selection ⌥⌘A
Column selection ⌥(drag)
Insert tag ∧⇑<
Wrap selection in tag ∧⇑W
Close tag ⌥⌘.
Wrap selection as link ∧⇑L
Subversion menu ∧⇑A
New method name∧↵

Commands (⇑⌥⌘C)

Run file as Ruby ⌘R
Start IRB ∧⇑I

Snippets (⇑⌥⌘S)

class class_name class➾
def method_name def➾
each { |method_name| } each➾
for element in collection forin➾
if condition if➾ or elsif➾
unless condition unless➾
:key => :value :➾
flash[:notice] = "…" flash➾
logger.info "…" logi➾
assert_equal value, @o.attr ac➾
redirect_to options rec[ai]➾
render :action => "action" ra➾
render :partial => "item" rp[loc]➾
create_table "table" mct➾
table.column :,: mcc➾
<%= link_to options lic[ai]➾
<% for item in @items eforin➾
<%= form_tag options ft➾
has_many :objects hm➾
belongs_to :object bt➾
validates_presence_of :attr vpif➾
=> ∧L
params[:id] ∧P
session[:user] ∧J
<% %> ∧X
<%= %> ∧Z
Nov 8

I just had a problem with my svn repository after a couple of plugins I had installed as svn:externals (./script/plugin install -x http://blahblah) were no longer required and I wanted to remove them. Not something I’ve done before, but I found a handy article here, and all you have to do is this:

cd vendor/plugins
svn propedit svn:externals . --editor-cmd vi

Remove the lines that reference the plugins you wish to remove, then save and quit vi (shift-ZZ if you didn’t already know).

Then svn commit and you’re done :)

Nov 6

Ok. Leopard is nice enough, but there are still a few niggles I have with it. One of those is the new dock.. shiny and cute as it is, I still prefer the old one. Fortunately the old (almost) one can be resurrected with these simple steps:

Edit: I’ve updated this article a little after some feedback from a friend of mine. Thanks Calum :-)

First run Terminal and type the following to make the switch:

defaults write com.apple.dock no-glass -boolean YES

Note: if you want to switch back to the shiny 3D dock, change that YES to a NO

Then restart the dock:

killall Dock

Tada!

Oct 31

AJAX loader image generator

Posted by Cliff

I was looking for a nice animated ‘loading’ image to make an AJAXified interface a little friendlier and I came across a handy little site. Once size obviously doesn’t always fit all, but if you want something quick and simple, try this.