Monday, November 18, 2013

Setting cell color using Ruby spreadsheet GEM

I've recently been working on a Rails project and needed to output some data in spreadsheet format.  My client wanted some simple styling of the spreadsheet but the standard colors didn't seem to fit the bill.  That's when I discovered that there are other options besides :red, :blue, :green, :cyan, etc. for the background style on a spreadsheet cell using the spreadsheet GEM.   Although I couldn't find any reference to these additional colors it is obvious from looking through the source code for the GEM that they exist.  Unfortunately, the symbol names used to represent these "additional" colors didn't lend themselves very well to visualize what color they represented.  They are :xls_color_0, :xls_color_1, etc...  and you can see them by looking at the source code here.

So I decided to output a quick spreadsheet with cell text representing the symbol name and then formatting the cell background with the color.   This makes a handy reference for those colors as well as providing some examples of how to format a cell.  Here's the source code:

The Gist is here.

The program  will output a file colors.xls that looks like the following:

Thursday, November 7, 2013

Capistrano deploy fails with gem hosted on

A college of mine and I recently upgraded on of our side projects from a Rails 3 and ruby 1.9.3 to Rails 4 and ruby 2.0.  In the course of doing so we needed to ensure that all the dependent GEM's we are using were Rails 4 and ruby 2.0 compatible.   As is usually the case someone had done the leg work and although not all of the core projects were updated, he was able to find forks on the original projects that had done the upgrades.

Today I was finally getting around to making sure the the application could still deploy to our staged environment.  Unfortunately it was failing.  I use Capistrano for the deployment and I was seeing it fail to clone the one of these GitHub hosted, upgraded GEMs my college found.  The error was:

Permission denied (publickey).
fatal: The remote end hungh up unexpectedly
Git error: command `git clone ''

I immediately tried to clone the project from my terminal window and all worked fine.  I thought, must be having an issue but upon checking that out on the status page they were not reporting any issues at all.  If I had paid more attention to the details all the clues I needed where right there in front of me but the actual problem still didn't dawn on me until I looked up this post.

Turns out the our GEMFILE needed to reference the non-SSH read-only URL for the projects so that I didn't need to authenticate with ssh.

Changed the entry in my GEMFILE from:

gem 'default_value_for', :git => ''


gem 'default_value_for', :github => 'tsmango/default_value_for'

Once that change was made bundle install was required but then I was able to successfully deploy our application.

Hopefully this saves someone else a few minutes of time troubleshooting similar errors.

Tuesday, October 22, 2013

Setup cruisecontrol.rb with rbenv and building with multiple ruby versions


I've found it useful to have my Rails projects running tests on changes to the source code repositories.  It helps to raise awareness to test failures.  I use cruisecontrol.rb for that purpose and have found it to be useful.  However, this post isn't about the merits of continuous integration, but rather about how to setup cruisecontrol.rb under rbenv and have it build your Ruby 2.0.0 projects.

Recently I started working on several Rails 4 projects and at the same time using Ruby 2.0. Unfortunately I ran into several issues with how I was using cruisecontrol.rb to build my projects which up until now have been Ruby 1.8.7 or 1.9.3.  Unfortunately, my cruisecontrol.rb setup was choking on Ruby 2.0.0 which was a little frustrating.  To simplify my setup a little bit I decided to move from RVM to RBenv to manage my rubies.  (side note: Move from RVM to RBEnv if you haven't, you'll probably thank me in the end) 

For this post I'm NOT going into how to setup CruiseControl.rb as there is already ample sources for that information.  The same is true for setting up rbenv as you can also find many sources of information for that installation as well.  If you want to start off with some sources I've used here they are:



How to Configure CruiseControl.rb to use rbenv and build a Ruby 2.0.0-p247 project

NOTE:  The following assumes you have a working cruisecontrol.rb setup and in addition you're using rbenv to manage your ruby versions.

First thing you'll need to do is change the cruisecontrol configuration file for your Ruby 2.0.0 project.
Open the file cruise_config.rb located in your .cruise/projects/(project name) folder, with your editor of choice and add the following line:

project.use_bundler = false

This tells cruisecontrol.rb not to use bundler automatically for your project.  This is important because you have cruisecontrol.rb running under Ruby 1.8.7 or Ruby 1.9.x and it will cause bundle failure. (side note: I'm running cruisecontrol.rb under 1.9.3-p327 for a year now and it works well)  

You'll also want to make sure you are using your own project build command.  You should include a line (or un-comment the one in the default file) so that it looks something like the following:

project.build_command = '../build_(your project).sh'

I keep my build file in the same directory as the cruise_config.rb file which is immediately above the work folder cruise control uses to build your project.

So you're cruise_config.rb for the project should look something like this at the end of the file.

After you've made that change you'll need to create or modify your shell script for building your project.   My build script looks like this:

It's important that you include the RBENV_VERSION line in your script.  This identifies the correct ruby version for your build.  Also, note the line  rbenv exec bundle install  as it is important for your setup to get the bundle setup correctly since you've told cruise control not to use bundler.   Your basically forcing the bundle to run using the specific ruby version.   I needed this because my Gemfile specified 2.0 and without it and the RBENV_VERSION line I was seeing this:

I have the lines rbenv and rbenv versions in my build file so that I can see the log file contains the correct version information.  This was helpful while troubleshooting this setup.

It's also important to note that since you are using bundler, instead of cruisecontrol.rb using it, that you might find that your project already has a .bundle/config file that has identified where your bundle should be stored.  You might want to change that file.  I had to in order to make sure that the bundle wasn't setup globally for all projects.

Hopefully this helps to get you up and running building Ruby 2.0.0 projects with cruisecontrol.rb

Wednesday, September 25, 2013

Postgres SQL connection error - Is the server running locally...

I ran into an issue on my Mac Mini today while setting up for a development footprint for a Rails 4 project I'm starting to work on this week.   This new project requires Postgres which I have not used before.  I ran into a snag installing it on my Mac Mini which is running OS X version 10.8.5

I proceeded to use homebrew to install by preforming these steps:

brew update
brew install postgres

In typical fashion the recipe presented some notes about continuing the setup.  The next steps were:

initdb /usr/local/var/postgres -E utf8
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Then I needed to use the postgres client to create a user and new database.  When I issued the command

psql -d template1

I received the following error:

psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

The initial indication here is that postgres wasn't running.  This post suggests verifying that by using the command:

ps auxw | grep post

and confirming that postgres was actually running.  In my case it definitely was running.  Several processes were matched.

Then I stumbled across numerous post suggesting that the PATH was set wrong.  That possibly Mac OS X Mountain Lion actually has pre-installed a postgres client that looks in a different place for the connection socket.  I don't know if that's true or not.  However,  unfortunately for me, it turns out that I was actually running an older / different version of the client located in /usr/bin  instead of the newly installed on from homebrew located in /usr/local/bin

You can check by the command:

which psql

and for me that result was /usr/bin

In my case the /usr/bin directory is in my PATH prior to the /usr/local/bin so rather than reverse that (like I probably should) I opted not to have any other surprises by a PATH change and simply copied the new psql client from /usr/local/bin to /usr/local

Hopefully this helps someone recover from this issue quickly.

Tuesday, September 10, 2013

dlload for Highline::SystemExceptions::WinAPI

Ran across an issue with 1.6.19 of the highline GEM recently.  I'm supporting a cross platform development environment for two applications done in RoR.  I'm primarily working on Mac OS X while another developer is working under Windows.  We are using Ruby 1.8.7 on these applications.

I recently updated to a newer version of Capistrano and but when my colleague tried to use this update on her Windows setup she ran into an error starting up WEBrick

... system_extensions.rb:81: undefined method for 'dllload' for Highline::SystemExceptions::WinAPI:Module (NoMethodError)

Several posts suggested to update to a newer Ruby version which isn't currently an option.  It also appears that there will be other issues taking that route anyway.  So instead I opted to downgrade highline to version 1.6.12 which was reported in one post to resolve the issue.

To downgrade just update your Gemfile to use the specific version of highline.

# Force to specific version of highline to fix issue on
# Windows OS with highline version 1.6.19
gem 'highline', '1.6.12'

Then use bundle:

bundle update highline

This should get you up and running again.

Friday, August 23, 2013

Subtle parse error for database.yml file

Subtle parse error for database.yml file

Recently I was setting up capistrano for a new Rails 3.x application deployment.  In typical fashion I was adjusting the database.yml file for a new environment but subtly introduced and error.  

The specific error was:

... did not find expected key while parsing a block mapping at line 4 column 1 ...

The short answer is:  Indentation of your YAML is likely off.

At first, the problem wasn't obvious so I ran to Google for help.  My search turned up the following post on which has the answer but still required close inspection of the accepted response.

New to me but there is also a site you can use to confirm your YAML:
(note: It should be obvious but I'll spell it out, don't past YAML with your passwords into the lint site)

Hopefully this post helps you resolve something quickly.

Friday, February 1, 2013

Microsoft Keyboard with Mac OS X

Boring story...

(you can skip this part)
I've always favored using the best tool for the job at hand.   This is something I learned along time ago when working with my grandfather in his workshop.  If you have a nail then the tool to use is a hammer.  Probably not just any hammer either, there are actually hammers designed to drive nails and ones that aren't.   Of the ones designed to drive nails there are different sizes and different manufacturers.  Sometimes the manufacturer makes a difference and sometimes it doesn't.  I've never forgotten those years and I always have looked to apply those same principals in choosing technologies to work on various software projects during my 28 years developing software commercially.  That's what left me trying to use the keyboard I have grown quite use to using with the new Mac Mini that I just recently started using.   In this instance writing code is the nail and my hammer of choice is the...

Microsoft Natural Ergonomic Keyboard 4000 v1.0

It is perfect for me.  Unfortunately, even with the Microsoft supplied Mac OS X Intellipoint device drivers the keyboard was frustrating to use on the Mac Mini.  The most frustrating point was the number pad NumLock key.  It simple didn't function as a NumLock.  The keypad on this keyboard when attached to the Mac OS X 10.8 (Mountain Lion) operating system was simply always in number mode.  I use the number pad to navigate around my code and it simply would no longer work.  I was frustrating myself trying to use the keyboard.  I would edit code and constantly being inserting 2's, 4's, 6's and 8's.   What a mess.   Countless searches trying to find a solution turned up many people who shared my problem but NO solutions.  I started to contemplate,  was it time for a new hammer?   I think not!

As luck would have it I found an answer!


We despite the title it worked for my Mac Mini.  In no time at all this genius little utility fixed the issue for me by making the NumLock key work.  I was so happy I donated to his cause so he could have a couple beers on me.

If you decide to use the utility to solve this particular problem I can save you some time looking through the hundreds of settings available with this utility.  Once you have it installed.  Go to System Preferences -> Other -> KeyRemap4MacBook  then choose the section labeled "For PC Users" and you'll see a few different options.  The one you want is:

"Use clear key to toggle "Keypad as Arrow (PC Style Home/End/PgUp/PgDn)"

In addition to that setting there are a few others under this same section that will likely help you out if you're like me and spend a majority of your day using a Windows OS.

As always I hope this helps someone with the same issue I was having.