Hobo Lifecycles

2010/05/06 at 9:09 pm (Hobo, Programming, Ruby on Rails, WebApplication)

Actually I discovered Hobo lifecycles a few days ago but they are a great thing. I knew they exist for some time now but I didn’t realize how common lifecycles are. Hobo manual entry on lifecycles uses example of friendship invitation. It’s a good example because it gives opportunity to show most of the lifecycles features. However I got impression lifecycles are for doing things that are at least a bit complex. But they’re not :)

Before my code snippet a word of explanation: “friends” in my app are not friends like on social networking sites (other users that you know or pretend to know). These are more like contacts in address book (you collect some data about them).

My first Hobo lifecycle has one state, one creator and three transitions. I’m not sure if it is proper use of lifecycles or example of abusing (note: now I think it was abusing) them but here it is:

lifecycle do
state :existing

create :new_friend, :params => [:name, :email, :balance], :become => :existing,
:available_to => “User”, :user_becomes => :owner

transition :remove, {:existing => :destroy}, :available_to => :owner

transition :change, {:existing => :existing}, :params => [:name, :email, :balance],
:available_to => :owner

end

No much here, huh? Just creating, editing and editing of “friend”… But it does one more thing: it handles ownership with even less typing then Hobo Permissions.

First a word of explanation: “friends” in my app  are not friends like on social networking sites (other users that you know). These are more like contacts in addressbook (you collect some data about them).
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: