Cheater

Like many web developers today I work with a variety of technologies to deliver a compelling product. Technology frameworks like Rails do a pretty good job of abstracting the more complex bits to allow the developer to focus on building the core functionality of the web app. Unfortunately, the typical web stack still has a lot of moving parts, so as you jump from the various languages and technologies it can be difficult to remember specific syntax or commands. So how do you remember everything?

Why worry about remembering everything..that is what your computer is for!

When developing I usually have several terminal windows open for running unit tests, REPL, db consoles, etc. The problem is if I need to remember a specific syntax or command for something I usually have to open up a browser window to do quick look up, which can really break my development flow.

Cheat sheets are great ways to remember those types of things and for years my cheat sheets have been text files that I would a grep for looking up notes. That approach worked, but was a little cumbersome. So I decided to throw a little Ruby at the problem and what emerged was Cheater, a simple Rake based tool for looking up cheat sheets for common code & console commands.

Cheater uses rake tasks to read YAML files that contain the cheat sheet commands & descriptions to print to your terminal window.

$ rake cheater:docs
Available Cheat Sheets
 rake cheater:capybara # Cheat Sheet for capybara.
 rake cheater:git # Cheat Sheet for git.
 rake cheater:rspec # Cheat Sheet for rspec.
$ rake cheater:capybara
CAPYBARA Navigating
 visit('/projects') # Navigates to given path.
 visit(post_comments_path(post)) # Navigates to given path; named route.
 current_path # Path of the current page, without any domain information; i.e. current_path.should == post_comments_path(post).

CAPYBARA Clicking links and buttons
 click_link('id-of-link') # Clicks link based on id of the anchor.
 click_link('Link Text') # Clicks link based on the text of the anchor.
 click_button('Save') # Clicks the button based on the text.
 click_on('Link Text') # Clicks on either links or buttons.

...

$ rake cheater:git
GIT CREATE COMMANDS
 git clone ssh://user@domain.com/repo.git # Clone an existing repository.
 git init # Create a new local repository.

GIT LOCAL CHANGES COMMANDS
 git status # Changed files in your working directory.
 git diff # Changes to tracked files.
 git add # Add all current changes to the next commit.
 git add -p  # Add some changes in  to the next commit.
 git commit - a # Commit all local changes in tracked files.
 git commit # Commit previously staged changes.
 git commit --amend # Change the last commit.

GIT COMMIT HISTORY COMMANDS
 git log # Show all commits starting with newest.
 git log -p  # Show changes over time for a specific file.
 git blame  # Who changed what and when in 

...

$ rake cheater:rspec
RSpec::Expectations Equivalence
 actual.should eq(expected) # Passes if actual object == expected object.
 actual.should == expected # Passes if actual object == expected object.
 actual.should eql(expected) # Passes if actual.eql?(expected).

RSpec::Expectations Identity
 actual.should be(expected) # Passes if actual.equal?(expected).
 actual.should equal(expected) # Passes if actual.equal?(expected).

RSpec::Expectations Comparisons
 actual.should be > expected # Passes if actual is greater than expected.

To add to cheater you simply create a new cheater_[cheatsheet].yml file and drop it into the cheater directory. Then from your terminal run rake cheater:[cheatsheet].

git clone https://github.com/CarlosGabaldon/cheater

Building a Rails Gmail Client Outside-In

New Article: Building a Rails Gmail Client Outside-In/

For all the new projects that I start with my stakeholders I have been pushing Outside–in software development and Specification by example. Specification by Example really improves the collabora…

Rendering Partials with Sinatra

Simple way to render partials with Sinatra:
https://gist.github.com/119874#gistcomment-238742

Ruby 1.9.3 IMAP segfault with OpenSSL

After upgrading to Ruby 1.9.3 I spent most of the morning trying to figure out why my IMAP code was giving a segfault

..
1.9.3p0 :001 > require 'net/imap'
 => true 
1.9.3p0 :002 > m = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
/Users/carlos/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/imap.rb:1439: [BUG] Segmentation fault
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.3.0]

-- Control frame information -----------------------------------------------
c:0028 p:---- s:0108 b:0108 l:000107 d:000107 CFUNC  :connect
c:0027 p:0198 s:0105 b:0105 l:000104 d:000104 METHOD /Users/carlos/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/imap.rb:1439
c:0026 p:0172 s:0100 b:0100 l:000099 d:000099 METHOD /Users/carlos/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/imap.rb:1036
c:0025 p:---- s:0091 b:0091 l:000090 d:000090 FINISH
c:0024 p:---- s:0089 b:0089 l:000088 d:000088 CFUNC  :new
c:0023 p:0023 s:0083 b:0083 l:001b28 d:001470 EVAL   (irb):2
c:0022 p:---- s:0080 b:0080 l:000079 d:000079 FINISH
c:0021 p:---- s:0078 b:0078 l:000077 d:000077 CFUNC  :eval
..

Turns out it was related to OpenSSL and the fact that I forgot to install the Ruby OpenSSL extensions. Chris Irish’s post reminded me of the requirement.

$ rvm remove ruby-1.9.3
$ rvm install ruby-1.9.3 --with-openssl-dir=/opt/local --with-iconv-dir=$rvm_path/usr

..

1.9.3-p0 :003 > m = Net::IMAP.new('imap.gmail.com', 993, true, nil, false)
 => #<Net::IMAP:0x007f808500cc30 @mon_owner=nil, @mon_count=0, @mon_mutex=#, @host="imap.gmail.com", @port=993, @tag_prefix="RUBY", @tagno=0, @parser=#, @sock=#, @usessl=true, @responses={}, @tagged_responses={}, @response_handlers=[], @tagged_response_arrival=#<MonitorMixin::ConditionVariable:0x007f808500c280 @monitor=#, @cond=#<ConditionVariable:0x007f808500c258 @waiters=[], @waiters_mutex=#>>, @continuation_request_arrival=#<MonitorMixin::ConditionVariable:0x007f808312c1d0 @monitor=#, @cond=#<ConditionVariable:0x007f808312c1a8 @waiters=[], @waiters_mutex=#>>, @idle_done_cond=nil, @logout_command_tag=nil, @debug_output_bol=true, @exception=nil, @greeting=#<struct Net::IMAP::UntaggedResponse name="OK", data=#, raw_data="* OK Gimap ready for requests from 174.19.150.134 7if4443230pbt.12\r\n">, @client_thread=#, @receiver_thread=#, @receiver_thread_terminating=false> 
1.9.3-p0 :004 > 

Rails Common Commands

A list of frequent Rails command line commands.

  • rvm use ruby-1.9.2-p0 - ruby version manager; switches to Ruby 1.9.2
  • rvm use ruby-1.9.2-p0 --default - ruby version manager; sets 1.9.2 as default
  • rvm use system - ruby version manager; switches to Ruby 1.87
  • rails new - creates a new Rails application
  • rails server [s] - launches WEBrick web server
  • rails generate [g] - lists available generators
  • rails generate controller --help - provides usage documentation
  • rails generate model --help - provides usage documentation
  • rails generate migration --help - provides usage documentation
  • rails destroy controller [Name] - undo generate controller
  • rails destroy model [Name] - undo generate model
  • rails generate scaffold [Name] --skip --no-migration - scaffold skipping existing files
  • rake db:migrate - runs database migrations
  • rake db:test:clone - clones current environment's database schema
  • rake db:test:purge - empties the test database
  • rake routes - list of all of the available routes
  • rake -T - list of rake commands
  • git init - creates git repo
  • git add . - adds all files to working tree
  • git commit -m "Initial commit" - commits files to repo
  • git status - status of the working tree
  • git push origin master - merges local repo with remote
  • git checkout -b new-dev - creates topic branch
  • git checkout master - switches to master branch
  • git merge new-dev - merges new-dev branch into master branch
  • git checkout -f - undo uncommitted changes on working tree
  • git branch - list branches
  • git branch -d modify-README - deletes branch
  • git mv README README.markdown - renames files using move command
  • heroku create - creates app on Heroku servers
  • git push heroku master - pushs app on to Heroku servers
  • heroku rake db:migrate - runs database migrations on Heroku servers
  • heroku pg:reset SHARED_DATABASE --confirm [app name] - deletes database file
  • heroku db:push - transfer an existing database to Heroku.
  • heroku logs - get logs.
  • rails console - command line interface to Rails app
  • rails dbconsole - command line database interface
  • bundle install - installs gems from Gemfile

Rubyist OS X Dev setup

I just bought a shiny new MacBook Pro with a 2.4 GHz Core i5, 8 GB Memory, on OS X 10.63 and thought I would share my setup.

TextMate

Head over to http://macromates.com/ and download TextMate. Once we have installed TextMate we are going to configure it. From the TextMate menu go to:

TextMate >> Preferences >> Fonts & Colors. Then select the “Twilight” theme.

For smaller projects where we do the markup ourselves we are going to use Haml, which we will install shortly, TextMate does not include this bundle by default, so we need to add it. Open up terminal and enter the following:

cd /Applications/TextMate.app/Contents/SharedSupport/Bundles
svn co "http://svn.textmate.org/trunk/Bundles/Ruby%20Haml.tmbundle/"

Back in TextMate go to Bundles >> Bundle Editor >> Reload Bundles.

Terminal

Next on our list is to configure Terminal.

The first thing we want to do is setup the bash prompt for Git. This cool trick will show our checked out topic branch right at the prompt:

Open a Terminal session and type the follow:

sudo mate ~/.bash_login 

To configure we will copy the following into our .bash_login file:


   RED="\[33[0;31m\]"
     YELLOW="\[33[0;33m\]"
 	  GREEN="\[33[0;32m\]"
       BLUE="\[33[0;34m\]"
  LIGHT_RED="\[33[1;31m\]"
LIGHT_GREEN="\[33[1;32m\]"
      WHITE="\[33[1;37m\]"
 LIGHT_GRAY="\[33[0;37m\]"
 COLOR_NONE="\[\e[0m\]"

function parse_git_branch {

  git rev-parse --git-dir &> /dev/null
  git_status="$(git status 2> /dev/null)"
  branch_pattern="^# On branch ([^${IFS}]*)"
  remote_pattern="# Your branch is (.*) of"
  diverge_pattern="# Your branch and (.*) have diverged"
  if [[ ! ${git_status}} =~ "working directory clean" ]]; then
    state="${RED}⚡"
  fi
  # add an else if or two here if you want to get more specific
  if [[ ${git_status} =~ ${remote_pattern} ]]; then
    if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
      remote="${YELLOW}↑"
    else
      remote="${YELLOW}↓"
    fi
  fi
  if [[ ${git_status} =~ ${diverge_pattern} ]]; then
    remote="${YELLOW}↕"
  fi
  if [[ ${git_status} =~ ${branch_pattern} ]]; then
    branch=${BASH_REMATCH[1]}
    echo " (${branch})${remote}${state}"
  fi
}

function prompt_func() {
    previous_return_value=$?;
    # prompt="${TITLEBAR}$BLUE[$RED\w$GREEN$(__git_ps1)$YELLOW$(git_dirty_flag)$BLUE]$COLOR_NONE "
    prompt="${TITLEBAR}${BLUE}[${RED}\w${GREEN}$(parse_git_branch)${BLUE}]${COLOR_NONE} "
    if test $previous_return_value -eq 0
    then
        PS1="${prompt}➔ "
    else
        PS1="${prompt}${RED}➔${COLOR_NONE} "
    fi
}

PROMPT_COMMAND=prompt_func

Next let’s make terminal a little prettier. From the Terminal menu:

Terminal >> Preferences >> Startup; New window settings: "Homebrew".
Terminal >> Preferences >> Settings; Font select "Andale Mono 18pt".

Now that we have TextMate and Terminal configured we are going to setup our development tools.

Development Tools

XCode Tools

Go to http://developer.apple.com/mac/ and download the latest version of XCode. Once it is downloaded simply run the installer.

Git

Next we will download the git-osx-installer, then run it.

RubyGems

From terminal we need to update our RubyGems:

sudo gem install rubygems-update
sudo update_rubygems

SSH

Still at terminal we will run:

ssh-keygen -t rsa -C "cgabaldon@gmail.com"
cat ~/.ssh/id_rsa.pub | pbcopy

GitHub

Then we will navigate over to GitHub.com >> Account Settings to add our RSA key that we copied to the clipboard.

GitHubAccountSettings by Carlos Gabaldon
GitHubAccountSettings, a photo by Carlos Gabaldon on Flickr.

MySQL

We are next heading over to download mysql-5.1.47-osx10.6-x86_64.dmg, and then once downloaded run the installer.

After MySQL is installed we will install the MySQL preference pane which gets added to the OS X System Preferences. To install we simply double clicking on the MySQL.prefPane contained within the MySQL install package. This gives us a convenient way to start and stop our MySQL server from the System Preferences.

Back to terminal to add our newly installed MySQL server to our path:

cd
sudo mate .bash_login # add: export PATH=/usr/local/mysql/bin:$PATH

Then last, but not least we need to install our ruby mysql gem:

sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Sinatra

sudo gem install sinatra

Rails

sudo gem install rails

Other Gems

sudo gem install sequel
sudo gem install haml
sudo gem install do_sqlite3
sudo gem install cheat 
sudo gem install cucumber 
sudo gem install dancroak-twitter-search 
sudo gem install faker 
sudo gem install geokit 
sudo gem install giraffesoft-timeline_fu 
sudo gem install github 
sudo gem install heroku 
sudo gem install json 
sudo gem install mislav-hanna 
sudo gem install nokogiri 
sudo gem install passenger 
sudo gem install rack 
sudo gem install railsmachine 
sudo gem install RedCloth 
sudo gem install redgreen 
sudo gem install reek 
sudo gem install rest-client
sudo gem install right_aws
sudo gem install right_http_connection 
sudo gem install rr 
sudo gem install rspec 
sudo gem install technicalpickles-le-git 
sudo gem install thoughtbot-factory_girl
sudo gem install thoughtbot-paperclip 
sudo gem install thoughtbot-shoulda 
sudo gem install webmat-git_remote_branch 
sudo gem install webrat 
sudo gem install wirble 

Capistrano

To simplify deployment we need to install Capistrano:

sudo gem install capistrano
sudo gem install capistrano-ext

FireFox

We of course need to install FireFox, by downloading and installing.

FireBug

Then Firebug and YSlow to help us on our front end engineering work.

Quicksilver

To help us keep our hands off the mouse we will install Quicksliver so we can exercise our keyboard foo. To install simply extract the tarball and drag to Applications.

Update the mappings to be Apple+Space for Quicksilver and Option+Space for Spotlight.

Fluid

Finally, we will install Fluid to help us turn our commonly visited sites into apps. Once downloaded drag to Applications.

Virtual Box

All production applications get deployed to a LAMP server, so we need a place to test out various configurations locally. Say hello to virtualization! We are going to use VirtualBox; download and install.

Evernote

Never forget anything; install Evernote and sign up.

Concentrate

Multitasking does not work when trying program, distractions will kill you. We are going to install Concentrate to apply the Pomodoro technique to keep focused and productive.

Things

We have a lot of Things to get done, so we need a great task management tool.

Growl

Most of the cooler OSX applications support notifications, Growl is the system for doing that.

Pencil

Pencil is one of the coolest UI prototyping tools I have used since a Sharpie.

Hack

We are now ready to do some serious hacking..

My Interview on RubyLearning blog

I recently had the pleasure of being interviewed by Satish Talim for his RubyLearning Blog on his mini series – “How do I learn and master Sinatra?” – by top Rubyists using Sinatra.

The interview series provides insight and commentary from notable Sinatra developers, with the goal of facilitating and providing answers to the questions Ruby beginners face on how to learn and master Sinatra.

Satish Talim is a programmer, author, trainer, and speaker. A recognized expert in the field of software development with over 30+ years of I.T. experience, Satish has consulted and trained teams at various companies in India and the US.

http://rubylearning.com/blog/2009/07/21/carlos-gabaldon-how-do-i-learn-and-master-sinatra/

Sinatra rake tasks

Since I have been playing around with Sinatra again, I decided that what Sinatra needs is some automation for some of the boring day to day tasks. So I created a GitHub bucket to dump my Rake tasks. I only have 1 task, to create a new project, but I have a lot new projects that I will be doing in Sinatra in the coming months, so I know there will be a cornucopia of tasks.

Sinatra-rake-tasks

More on Sinatra

I wrote another article on the cool Ruby web framework DSL Sinatra.

In my last article I wrote about the cool Ruby DSL web framework called Sinatra which is taking the Ruby world by storm. I decided that another “How to” article on some of Sintra’s other kick ass features was just what Frank would expect.
..

Read More..