W3C logger plugin for Rails
I just released a plugin for Rails that logs your rails requests in W3C format. Checkout http://github.com/primedia/w3c_logger/tree/master.
Installing Ruby 1.9 on Mac OS X 10.5
Tommie over at The blog with no name has written up a how-to on getting Ruby 1.9 up and running in Mac OS X 10.5.7.
Check it out: http://www.tommycampbell.net/2009/06/26/ruby-1-9-on-mac-os-x
How to optionally load gems
I ran into an interesting problem today at work. I wanted to optionally require a ruby gem. For example, I wanted to include the metric_fu rake tasks, but if a user doesn’t have metric_fu installed, it’s no big deal.
Here’s how I implemented it. I added a method to the Kernel space (where require lives) called desire. I desire this gem to be installed, but I should just see a warning if it’s not installed, instead of an error.
Here’s the desire method (I put this in RAILS_ROOT/Rakefile).
[ruby]
module Kernel
def desire(library)
require library
return true
rescue LoadError
STDERR.puts "warning: #{library} gem desired but not found."
return false
end
end
[/ruby]
And here’s how I optionally include the metric_fu gem.
[ruby]
desire ‘metric_fu’
[/ruby]
If a user has the gem installed, rake -T will show the tasks.
[text]
jasonn:~/sources/my_files [master] $ rake -T
(in ~/sources/my_files)
rake metrics:all
[…]
jasonn:~/sources/my_files [master] $
[/text]
If the user does not have the gem installed, rake -T will show a simple warning and continue.
[text]
jasonn:~/sources/my_files [master] $ rake -T
(in ~/sources/my_files)
warning: metric_fu gem desired but not found.
[…]
jasonn:~/sources/my_files [master] $
[/text]
I don’t remember where I first heard the desire idea, but it works pretty well.
Recover files from git after deletion is committed
As I work with git, I have occasionally deleted files that I later end up needing. You can restore the files, but it’s not an intuitive process.
First, I looked at the output of git log to find the commit that I deleted the files with.
Then I ran:
[text]
git diff –diff-filter=D –name-only HEAD@{‘7 days ago’} | \
grep -v ^doc | perl -ne ‘chomp;
print "git show e2ae03f14ce81a3e24ec6cbe2e73767f4c6fed1b — $_ > $_\n";’ | sh
[/text]
This restored all the files I deleted that did not start with doc.
These files show up as diff files, so you may have to remove the “- ” at the beginning of the lines and commit messages. But hey, it’s better than not having the files you need.
RDoc preview in Textmate
While working on writing some RDOC documentation within Textmate, Rein Henrichs and I ran into a problem. Textmate will give you previews if you use markdown, but not RDOC. Below is how to do it.
Create /usr/bin/rdoc2html:
[text]
#!/usr/bin/env ruby -rrdoc/markup/simple_markup -rrdoc/markup/simple_markup/to_html
input = ARGF ? ARGF.read : STDIN.read
print SM::SimpleMarkup.new.convert(input, SM::ToHtml.new)
[/text]
Make the file executable:
[text]
sudo chmod +x /usr/bin/rdoc2html
[/text]
In textmate, hit Cntrl-Option-Command-P. In the bottom left corner, click Show options. Click the Pipe text through checkbox and enter “/usr/bin/rdoc2html” in the text box.
GitHub set author/committer
I’ve been working with Git lately and more specifically GitHub. I noticed something odd one day. Where most commits have one author listed, these commits had both a author and a committer listed.
More specifically, the author field was the two members of the pair and the committer name was the workstation they were working on.
Example:
After playing around with it for a while, I figured out how to do it. Within a git project clone, run the following commands.
1) git config user.name = ‘Workstation Perlwizard’
2) git config user.email = ‘devs2@perlwizard.org’
3) export GIT_AUTHOR_NAME=’Bob Smith and Betty Jones’
4) export GIT_AUTHOR_EMAIL=’devs@perlwizard.org’
Both of the email addresses need to be addresses Github doesn’t know about (i.e. not used to sign up for an account).
I could see using this while pairing with someone so you both get name credit for your commits.
Making TextMate recognize .html.erb files
I’m using TextMate to edit some Ruby on Rails code. I noticed that when I edit the view pages (i.e. index.html.erb) it does not do syntax highlighting. It turns out this is because the Ruby on Rails TextMate bundle thinks that ruby view pages are .rhtml, which is the old Ruby on Rails format.
You can fix this by editing the Ruby on Rails language bundle.
In TextMate, select Bundles -> Bundle Editor -> Edit Languages.
This will open a new Bundle Editor Window. Click the arrow next to Ruby on Rails and highlight HTML (Rails). In the code that appears on the right, change the following
[text]
fileTypes = ( ‘rhtml’);
[/text]
to
[text]
fileTypes = (‘rhtml’, ‘html.erb’);
[/text]
Click the red X in the upper left corner to close the Bundle Editor Window.
Close any existing .html.erb files, when you open them again, they will have ruby syntax highlighting.
Adding an alias to the Tcsh Shell
If you run a command frequently, you can create what is called an alias. The alias is usually a shorter form of the original command.
To make these aliases permament, you should edit .tcshrc in your home directory.
[text]
11:32:07 /Users/jasonn $ vi ~/.tcshrc
[/text]
Add a line of text of the format “alias YYY ‘ZZZ'” where YYY is the command you want to type in, and ZZZ is the command you want to be run.
Examples:
[text]
# Show long listing
alias ll ‘ls -al’
# Use VI Improved instead of Vi
alias vi ‘vim’
# Change into your web directory
alias web ‘cd /usr/local/apache/vhosts/jasonnoble.org/htdocs’
[/text]
This alias ability allows you to type less at your shell prompt. If you find yourself typing the same long command line over and over, try adding an alias to your shell that will save you some time.
Adding an alias to the Bash Shell
If you run a command frequently, you can create what is called an alias. The alias is usually a shorter form of the original command.
To make these aliases permament, you should edit .bashrc in your home directory.
[text]
11:32:07 /Users/jasonn $ vi ~/.bashrc
[/text]
Add a line of text of the format “alias YYY=’ZZZ'” where YYY is the command you want to type in, and ZZZ is the command you want to be run.
Examples:
[text]
# Show long listing
alias ll=’ls -al’
# Use VI Improved instead of Vi
alias vi=’vim’
# Change into your web directory
alias web=’cd /usr/local/apache/vhosts/jasonnoble.org/htdocs’
[/text]
This alias ability allows you to type less at your shell prompt. If you find yourself typing the same long command line over and over, try adding an alias to your shell that will save you some time.
Adding an alias to the Tcsh Shell
If you run a command frequently, you can create what is called an alias. The alias is usually a shorter form of the original command.
To make these aliases permament, you should edit .tcshrc in your home directory.
[text]
11:32:07 /Users/jasonn $ vi ~/.tcshrc
[/text]
Add a line of text of the format “alias YYY ‘ZZZ'” where YYY is the command you want to type in, and ZZZ is the command you want to be run.
Examples:
[text]
# Show long listing
alias ll ‘ls -al’
# Use VI Improved instead of Vi
alias vi ‘vim’
# Change into your web directory
alias web ‘cd /usr/local/apache/vhosts/jasonnoble.org/htdocs’
[/text]
This alias ability allows you to type less at your shell prompt. If you find yourself typing the same long command line over and over, try adding an alias to your shell that will save you some time.