Namespaces and Rake Command Completion

Update: Now using rake and caching

I got some basic rake command line completion working today using Jon Baer’s comment. Very simple, very easy:


complete -W ‘rake—silent—tasks | cut -d ” ” -f 2‘ -o default rake

However this didn&#8217;t work for the namespaced tasks in a rails app like <code>rake test:units</code>.  Searching a little further I found a reference to some code <a href="http://i.conio.net/rake-completion.rb">Nicholas Seckar</a> wrote on <a href="http://project.ioni.st/post/212">project.ioni.st</a>.  This used ruby to find the possible tasks for command completion.  This looked promising, but it still didn&#8217;t work for namespaced tasks.  A little more googling led me to what looked like the perfect link: <a href="http://www.webtypes.com/2006/03/31/rake-completion-script-that-handles-namespaces">Rake-completion script that handles namespaces</a>.  Alas, it only handled one level of namespacing.  It worked nicely for <code>rake test:units</code>, but <code>rake tmp:ses<TAB></code> would complete to <code>rake tmp:clear</code> instead of <code>rake tmp:sessions:clear</code>.  Also, <code>rake test:units <TAB></code> would complete to <code>rake test:units units</code> instead of giving me all the tasks again, just in case you want to run multiple tasks form the command line.


So, now what?  Stand on the shoulders of others, naturally.  Here is what I&#8217;m using now that handles multiple namespaces and multiple tasks per command line:


#!/usr/bin/env ruby

Complete rake tasks script for bash

Save it somewhere and then add

complete -C path/to/script -o default rake

to your ~/.bashrc

Nicholas Seckar nseckar@gmail.com

Saimon Moore saimon@webtypes.com

http://www.webtypes.com/2006/03/31/rake-completion-script-that-handles-namespaces

exit 0 unless File.file?(File.join(Dir.pwd, ‘Rakefile’))
exit 0 unless /^rakeb/ =~ ENV[“COMP_LINE”]

after_match = $’
task_match = (after_match.empty? || after_match =~ /s$/) ? nil : after_match.split.last
tasks = rake --silent --tasks.split(“n”)[1..-1].collect {|line| line.split[1]}
tasks = tasks.select {|t| /^#{Regexp.escape task_match}/ =~ t} if task_match

handle namespaces

if task_match =~ /^([-w:]+:)/
upto_last_colon = $1
after_match = $’
tasks = tasks.collect { |t| (t =~ /^#{Regexp.escape upto_last_colon}([-w:]+)$/) ? “#{$1}” : t }
end

puts tasks
exit 0

<a href="http://onrails.org/files/rake-completion.rb">Enjoy</a>

Dave Thomas and Mike Clark – Ruby on Rails Podcast

A clarification of what it means for Rails to enter the enteprise, and a preview of the upcoming Rails Edge conferences.
Also mentioned:

Dave Thomas and Mike Clark – Ruby on Rails Podcast

A clarification of what it means for Rails to enter the enteprise, and a preview of the upcoming Rails Edge conferences.
Also mentioned:

Jeff Cohen and Brian Eng – Ruby on Rails Podcast

The authors of the Softies on Rails blog talk about developing Rails apps on the Windows platform.
Also mentioned:

Jeff Cohen and Brian Eng – Ruby on Rails Podcast

The authors of the Softies on Rails blog talk about developing Rails apps on the Windows platform.
Also mentioned:

Full time Ruby On Rails Job in Denver.

One of my customers in Denver is looking for an experienced full-time Rails developer. The company is called , Gatelys/Etail Source. You will be part of a small, experienced, and agile development team that has developed three pretty cool Rails eCommerce applications. This is the posting they published on the job board of 37signals (http://jobs.37signals.com/jobs/323):

Description

Small (but quickly growing) eCommerce startup seeks enthusiastic full-time senior-level Rails developer / system engineer. Special focus on performance optimization and tuning. Denver area resident preferred, but not mandatory. Salary will be competitive, depending on qualifications.
To apply

Send resume and salary requirements to jobs@gatelys.com, attention Solomon.

Short circuiting partials

I tend to think of partials as mini-components. I don’t like it when the page using the partial “knows” too much about what’s in the partial. For instance, if your partial displays a collection of things but shouldn’t display anything if the collection is empty, then you’ll usually see something like this:


outer page stuff…
<% unless some_object.has_many_things.empty? %>

My collection of stuff

<%= render :partial => ‘my_partial’, :locals => { :collection_of_stuff => some_object.has_many_things } %>
<% end %>
more outer page stuff…

The outer page has to know which collection of things the partial is going to use and performs some display logic around that. Shouldn’t this all be contained in the partial? The outer page should just know that it is going to use a partial and that’s it. So our outer page will look like this:


outer page stuff…
<%= render :partial => ‘my_partial’, :locals => { :some_object => some_object } %>
more outer page stuff…

Now it looks like a self-contained component. Good. Of course, we now have to put that outer display logic in the partial.


<% unless some_object.has_many_things.empty? %>

My collection of stuff

<% some_object.has_many_things.each do |o| %>
do some stuff with <%= o %>
<% end %>
<% end %>

Now the messy part is the big unless block around the whole partial. I’d like to just short circuit the whole page if there’s nothing to display, similar to what I do in normal methods:


def do_something(some_arg)
return nil unless some_arg
do_something_cool_with_some_arg
end

Can it be as simple as putting a return statement in our partial? It turns out, yes, it is that easy.


<% return if some_object.has_many_things.empty? %>

My collection of stuff

<% some_object.has_many_things.each do |o| %>
do some stuff with <%= o %>
<% end %>

Wow! One whole line shorter. It doesn’t look like much in this small contrived example, but I think it makes the purpose of the partial clearer and doesn’t clutter up the calling page. Give it a shot.

Dreamhost out, Slicehost in.

I must say I like Dreamhost but we had too much downtime recently. Even if we offer time.onrails.org for free it must be up and running when the users need it. This is especially true for a time management application. When you are done with your workday, one click and go home. Lee and I are using time.onrails.org daily for all of our customer projects and recently it has been down once too many time. In addition it’s getting really slow, but then again with a $9 a month shared host plan, I must admit we went already a long way. So time to move on and find a better solution.

We went the last two days on the search for a new hosting provider that would provide dedicated or virtual private servers. There is lots of offerings out there and good information. Lee stumble upon slicehost.com and liked the idea of a small hosting company, that diggs Ruby on Rails, and is about to be launched. They are small and hopefully will work hard to make it. I was not too convinced so we decided to contact them, and had a nice chat with them over their campfire support room. See the campfire transcript. I gave them a hard-time, but they provided us with lots of good information. Check them out if you want to find out what’s going on at slicehost

Transcript: http://onrails.org/files/20060804_slicehost.html

So time.onrails.org users, thank for your patience. In the next month we will move over our application to a way faster environment.

Update: time.onrails.org has been moved. If you sign up with slicehost because of us, feel free to say we referred you. 🙂 Just use this referrel link.

Shugo Maeda – Ruby on Rails Podcast

A report from RailsConf with Japanese developer Shugo Maeda. Translated by Stephen Munday.

Shugo Maeda – Ruby on Rails Podcast

A report from RailsConf with Japanese developer Shugo Maeda. Translated by Stephen Munday.

RailsAnalyzer – visualize your Ruby on Rails production log

Download Application:
User Guide

RailsLogAnalyzer – help wanted

I need some Mac users to test and give feed back on a very early version of the RailsLogAnalyzer. This app is an OSX app and not deployed on a server, so I would like to find out the obvious bug that may occur on different Macs. I tried it on my own MacBook Pro, G5 server, and on an old PowerBook G4. But then again the people most likely to use it are Rails developers that may have differences in their environment.

So if you like to take risks :-), understand the basic structure of a Rails app, have a production.log you want to analyze, and are not afraid of some scary bugs then read on…