martedì 31 gennaio 2012

Vim color scheme samples

I have a lot of Vim colorschemes, and I like to change them very often, reflecting my mood. I needed a way to showcase them all and quickly pick one.

The original Vim Color Scheme Test script by maverick.woo is written in Perl and the build works on Windows systems. I wanted to add some new features, and to test it with my own colorschemes, but as I'm not very confident with Perl, I preferred to start over with a new Ruby version instead of forking his project. Here's my version (and here's the github page):

The script loads all your colorschemes from your default vim directory (~/.vim/colors), and writes into the output dir an HTML file for each colorscheme, with a render of a Ruby file using this colorscheme. It also writes a different copy for each language present in the samples/ directory. It also builds an index page for each language, with a showcase of how the colorschemes render the sample code, a download link for each colorscheme and a nice lightbox to preview it.

To make it run, you'll need:
  • ruby
  • macvim
  • tilt rubygem (to render the index template)

What still needs to be done:
  • Separate light and dark colorschemes
  • Make this work with versions of vim different from MacVim
  • Add the current language name to index pages
  • Add more languages (currently only Ruby and Python are supported)

ATM, the script uses a vim server named VIMCOLORS and sends it remote commands. This was made to make it faster, because opening a single macvim instance for each script required too much time. However, the --remote-send command of vim doesn't wait for previous remote-sends to be completed, so I had to add a "sleep 1" command in the script to prevent it from messing up the execution flow. Any hint to solve this is greatly appreciated.

venerdì 28 ottobre 2011

Using virtual attributes for multi-parameter form helpers in Rails

In a Rails application I am working on, I needed to setup a form with a field with a non-standard behaviour. The field represents a date object, so the FormHelper date_select helper looked great; however, the date to display was not the actual date to be set on the database, but the day before. Changing all the data on the DB was a bit risky, so I had to stick with this requirement.
I decided to use a virtual attribute to do this, as it seemed the most elegant solution, so I wrote this in my model:

And this in my controller:

However, trying to send this data to the controller resulted in a "1 error(s) on assignment of multiparameter attributes" error.

The solution I found after some search was this one:


mercoledì 20 luglio 2011

OSX Lion: "This disk cannot be used to start up your computer"

Today I downloaded the installer for OSX Lion from the App Store. I ran it, and when it came to choose the disk to install to, it told me that "this disk cannot be used to start up your computer". I had a setup with 4 partitions: the hidden EFI system partition, the Macintosh HD partition, and two linux partitions (an ext3 one + a swap one).

So, the solution was to resize the Macintosh HD partition. I booted my Mac from the Lion install DVD (you can burn it from the installer package, just search google to know how to do it), went into disk utility, checked all partitions for errors (this was necessary because I wasn't able to resize them otherwise) then I resized only the Macintosh HD partition shrinking it by some GBs.

After this, I ran the installer again and installation started smoothly.


domenica 19 giugno 2011

Machinist vs Factory Girl: Machinist win!

Today I decided to verify if Machinist could be a good replacement for Factory Girl. In our project, we have a big problem with Factory Girl: even if you tell her not to hit the database, using the method, if an object has associations, these are saved on the DB. And this causes a huge slowdown in specs using factories. We've been using Factory Girl for nearly two years, and if we could find a way to stop him hitting the DB, we could really have a huge improvent in our test suite running time.

To verify if Machinist could perform better, I set up a basic rails app. Look at this example:

If you do a tail -f log/test.log and you run this spec, you'll see something like this:

The method has to save dependencies on the DB to set the foreign keys on the objects and validate them.

Let's try with machinist:

This time, running tail on the test.log file and running the spec, doesn't shown any DB hit, and of yeah, we have a green test.

I verified this also by putting a debugger line after the validation and inspecting the DB from within the debugger after the validation has run - with FactoryGirl, it revealed an Address object saved on the DB, while with Mechanist it didn't.

I still haven't looked inside machinist to show how it handles this, but I'll do it soon, so keep in touch!

giovedì 10 marzo 2011

Howto: Run a rake task in sandbox mode

If you have a Rails rake task that somehow changes your DB data, but you want to be sure that the DB will be rolled back to its previous state after the rake task has completed, you can simply include this snippet right after your task definition:

If you wonder where is this code coming from, it's directly from the rails console code.

venerdì 28 gennaio 2011

Rails 3 scopes with HABTM (has and belongs to many) relations

There are already many posts about this, but maybe this simple example will help you understand this subject even better.

martedì 18 gennaio 2011

invalid option: --with-pg-dir=/opt/PostgreSQL/9.0

I'd bet a lot of ruby devs actually found themselves stuck in this problem. You checkout a github repo, you run a bundle install and = duh = a gem cannot install because of a missing library.
You're sure you've already installed the library or dependency or whatever, but in a different path from the standard one (in this example I'm talking about PostgreSQL installed via the graphical installer instead of the ubuntu apt repo); so you issue the command

gem install pg -v0.9.0 --with-pg-dir=/opt/PostgreSQL/9.0

And you get this error message: invalid option: --with-pg-dir=/opt/PostgreSQL/9.0

What's the problem? You need to separate options with another pair of dashes:

gem install pg -v0.9.0 -- --with-pg-dir=/opt/PostgreSQL/9.0

And everything will work.