Archive for May, 2007

Join the Malaysia Ruby Brigade!

Hence forth in the battlegrounds of the Internet, I stake a claim in various IP address ranges in the name of the Malaysia Ruby Brigade.

Malaysia Ruby Brigade

Joining the ranks of other Brigades around the world, Singapore, Seattle, Portland, a group of Ruby enthusiasts have come together to form a coalition to advance the state of Ruby in Malaysia.

It’s time to get in line soldiers!

Spread the word, the Malaysia Ruby Brigade is recruiting new personnel to clean the the barracks, wash the dishes, and drive me around.

Beware PHPers as we will cut through your spaghetti barrier defense, with our modular mix-ins!

Java-ians, you better watch out as we, we will enslave your notorious XML and inflict thee with they fury of the YAML. It will almost be like freeing you from the pain you stupidly inflicted on yourself.

To the Pythonista bretherens, even though we find you short sighted, you share a similar resemblance amongst us, as if we were raised together like brothers. We open doors to you to take up arms at Ruby Brigade. Though only at the lowest ranks, you get the opportunity to be target practice, and if you do really well you get to do our dirty laundry.

To the others, we gladly accept any challenge to bear arms!

Towards a purification of the source code!

Comments (1)

Ruby on Rails Finite State Machine Plugin: acts_as_state_machine

A finite state machine is a model of behavior with a finite number of states, interconnected via transitions and events.

In this guide, I will introduce you to a Ruby on Rails plugin to easily recreate a Finite State Machine with your models. The plugin we will be using is the acts_as_state_machine, which as you can see has very sparse documentation. Google does pull up some results, but not good enough, at least for beginners.

Why would you use a Finite State Machine?

For starters, if your model has a finite number of various states, and you want an easy way for callbacks to be done. Callbacks can be used to notify, validate, increment, anything, when your model changes state.

Installing acts_as_state_machine

Go to the root folder of your Rails application and execute:

./script/plugin install \
    http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/

Warning: Split over two lines as it was really long.
Don’t forget if your project is under subversion, you can use the -x flag to externally link to the plugin

Using acts_as_state_machine

Note: The plugin makes an assumption that the state of your model is saved in field called state. This can be replaced by adding the additional option :column => 'field'.
Warning: If you are using a model that stores addresses, be weary of a field called “state”. You can spend hours wondering why things aren’t working like they should be.

Personal Preference: I like to describe the states my models are in with as an adjective, and the event as a verb.

Notice how in line 2 we explicitly state the initial state of the model. In lines 3 to 6, we indicate the various states the Person may be in.

There is a peculiar behavior when creating objects via new, in that the model’s state is not specified. It will only be specified when saving the new record. One solution is to specify the default state from within the migration. The other solution then is to call create.

Example:

Note: If you didn’t notice the trend, the method to test if the model is in the state, is to append the state with a question mark: "state?"

The events you specified also creates instance methods, to transition the model from one state to another.

The following instance methods were created:

Note: The instance methods created follow the pattern "event!"

Events

Note: By calling any event, you also call ActiveRecord::Base.save. For when it fails, it only returns false. You can guard yourself by calling valid? and save!

Events help you to transition from one state to another. So suppose your person is sleeping, and you want him to shower, well we’ll just call shower!.

Events can help your organize the flow of your model. But they can get more powerful with callbacks.

Callbacks

The state also comes with a few callbacks that can be used.

Callbacks are called when the model is transitioning into the specified state.

Note:

  • Callbacks can be either a symbol or a Proc. If used as a symbol, the instance method of the model will be called
  • The callbacks act differently if the model is a new record and hasn’t been saved, versus an already saved model.

When put into consideration with ActiveRecord’s callbacks, a new record’s callback would look like this:

  • ActiveRecord::Base.before_save
  • ActiveRecord::Base.save
  • acts_as_state_machine :enter sleeping
  • acts_as_state_machine :after sleeping
  • ActiveRecord::Base.after_save

When the model is no longer a new record, the callbacks execute as follows, if I had called the shower! method.

  • acts_as_state_machine :enter showering
  • ActiveRecord::Base.before_save
  • ActiveRecord::Base.save
  • ActiveRecord::Base.after_save
  • acts_as_state_machine :after showering
  • acts_as_state_machine :exit sleeping

Guarding States

But how about if you want some sort of validation for a transition. You know, just to ensure data integrity.

The transition can be guarded by specifying a :guard option, with either a symbol or Proc (similar to the Callbacks). The method or Proc has to return true to proceed with the transition, else it will fail silently.

Conclusion

Well thats the basics, and as much as acts_as_state_machine does.

Any tips, tricks and or pointers? Leave them as comments.

Or are there any other interesting plugins you’d like me to explore?

Comments (27)

Ruby on Rails vs PHP and Java

The guys at Rails Envy, have put up videos in the style of Apple’s Mac vs PC ads, putting Ruby on Rails upfront vs heavy weight champions PHP and Java.

Via Youtube:

  1. Ruby on Rails vs Java
  2. Ruby on Rails vs PHP - Migrations
  3. Ruby on Rails vs PHP - Organization
  4. Ruby on Rails vs PHP- Changing Database

Comments and Critiques

Ah pitting Ruby on Rails vs the big two. Though at times it can seem like pointless bickering. Is this what these videos are?

The first video (vs Java) was good. I like the use of ‘jars’ and the various frameworks stored in them. Nice touch. The background music starts a bit late though.

In the second video (vs PHP Migrations), I wondered why the used the same guy for Java as for PHP. In my opinion bad choice. Though they did change the Java/PHP guy’s shirt…

The third video (vs PHP Organization) is not entirely. Sure there is no pre defined set on how to build your application, but good programmers do separate code from logic, and do use MVC via various frameworks. Similarly I feel the end (after they showed the Rails logo) was carried too long.

I felt the fourth one was simply weak.

Naturally they would use the Ruby on Rails logo. But it doesn’t carry a ‘professional’ touch that the Apple logo does. This is not a fault of the logo (which I think is great by the way), but might have been with the use of the serif font for the link ‘RubyOnRails.com’. Or maybe that the logo was just TOO big.

Ruby on Rails Video

My take. Smaller, with a reflection on the bottom, and the address in lowercase. The font could be better, and its in the way of the reflection. Just a quick example nonetheless.
Rails Envy

The video quality could have been a bit better. It may just have been their bad encoding, then uploading Youtube, which degraded the quality even more. But somehow it seems like it was taken off a normal home video camera.

Maybe it would help if the Rails duded didn’t look so nerdy. In the Apple ads, Apple wants the Mac guy to be perceived as cool, hip, hop, and very stylo. They are advertising a lifestyle. The Rails ads doesn’t do this, and it helps a lot. Basically put, It doesn’t look cool to use Rails. Actually the Ruby guy sounds very cocky. “Actually Ruby on Rails…”

When the final version of the Rails logo was released, it was compared as:

The remix signals less rigidness and more fun than the original—two important properties to Rails development.

Note: Emphasis by me.

In the same way, the ad doesn’t seem to portray to me that Rails development is “less rigid” and “more fun”. Sure it’s stated clearly by the actors, but you don’t get the sense of it.

Ok sure, it was a parody. But this is also comments and critiques. Doesn’t matter, as reading the comments people seem to love the ads anyways.

I wonder why ASP wasn’t considered? Or even Django?

Personally I prefer the various The Least Surprised videos about Ruby.

Comments (1)

« Previous entries · Next entries »