Photo
contributing to open-source feels good when others acknowledge your work.

contributing to open-source feels good when others acknowledge your work.

Text

useful stubs in cucumber

Just discovered you can stub out anything in Cucumber, even in Rails integration tests.

First you need to require “cucumber/rspec/doubles” to load mocking, stubbing, in your features/support/env.rb file. If you use spork, add it in the prefork.

Consider the following step definition:

Given /^today is "([^"]*)"$/ do |date|
  Date.stub(:today) { Date.parse(date) }
end

Use the step in a feature:

Given today is "15/06/2011" 

All Date.today calls produce that date :-)

Text

stubbing helper methods

This is a little odd.

In order to stub a method call in a helper you have the call the method you are testing on helper:

Example

it "should return true" do
  helper.stub(:weird?) { true }
  helper.weird?.should be_true
end
Text

using ActiveRecord to query times within a range

Just discovered this, something I wish I knew a LONG time ago.

Model.where(:time => start_date..end_date)

Which will generate the following SQL query (depending on the database):

SELECT "records".* FROM "records" WHERE "records"."time" BETWEEN '2012-03-13' AND '2012-03-14'
Text

getting quicker with vim by mapping a focus button

As you develop Ruby/Rails apps with Vim or any other app and doing it the TDD/BDD way.

You often find yourself doing :! cucumber features/spellcheck.feature

Every time you run code.

And it’s okay that the string is very long. You can repeat it by doing :<UP>

But what if you need to run a spec later on and then go back to running that feature.

Hmm now things are getting trickier too manage.

So I’ve come up with a little technique called the focus button.

You can do

:map <C-f> :!cucumber features/spellcheck.feature<Cr>

And that will run the cucumber feature by pressing CTRL+F.

Of course this isn’t limited to cucumber or ruby.

I also like to set this in my .vimrc

:map <C-f> :! rspec spec && cucumber<Cr>

So that at the start of a day, I can check in my app to see if anything if failing just by hitting CTRL+F. Then I rebind it as I get into it to focus.

Tags: vim ruby rails
Text

Sorry, thank you, and I’m back.

First of all, sorry for leaving my blog so unattended for a long period of time.

I was. Let’s just say going through a personal crisis. I almost went bankrupt :-)

Lessons learned: You need capital to start businesses. Even bootstrapped businesses. Don’t kid yourself.

On the positive note, it seems that some of my posts regarding BDD, Vim, Ruby, have attracted a little audience.

So with the beautiful assertion that people are interested in things I may have to say about agile development…

I’m back.

Thank you all once again for liking, following me, whatever. It helps.

Tags: thank you
Text

Finding text in your project files with Vim

So one thing that’s bothered me recently is how bad I was at handling large amounts of files.

Partly because I didn’t know how to find in all my files.

Now I do.

:help vimgrep

You gained 50 experience

Tags: vim
Text

This is the first in hopefully what will turn into a series. Each week, I focus on learning something. I share my discoveries through out the week. So expect at least another article about vim as my knowledge expands.

Topic: Taking control of vim

Introduction to my state on VIM

This week I’ll be focusing on improving my control over Vim. I’ve been using vim a lot recently as a replacement for TextMate in Mac OSX. Actually, MacVim. Then vanilla vim.

Here is a short summary of what I discovered recently.

  • I quickly found NERDTree annoying more than useful. Barely used it. It often crashed.
  • I like CommandT, but don’t love it.
  • I love splitting windows.
  • I love vim
  • I wasted a lot of time searching for that “Super VIM booster package”
  • I probably never used any of the things packaged in the packages I used

So after analyzing all of this, I have made it a personal goal of mine to really take a good full control over Vim.

I’ve decided to take full control of Vim in my life. I want to know how to install/update it, run it, configure it, write my own .vimrc.

There are two environments I will run vim in my life at the moment.

My macbook pro running Lion. My ubuntu desktop running inside a virtual machine on my windows desktop.

Since I’m right now on ubuntu, I’m going to talk about installing it.

There are two packages for vim. vim and vim-nox.

Install vim-nox and you can live care free.

If you do care, read this:

Installing Vim from source

So first I’m going to remove vim and vim-nox packages. If they are installed. rm any binaries left that are either vim or vi. which vi will tell you where your vi is.

Remove /etc/vim

Okay. Now I don’t know where else to look for vim stuff so I’m just going to assume I’ve removed it, hopefully anything we’ve broken is going to be repaired when we compile vim ourselves.

Now let’s install it. Let’s get the packages we need to compile this (Ubuntu users), OSX users probably need to install XCode.

apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Check out the vim page on downloading vim which will be more likely to have up-to-date information than this page.

And now that you have the vim source code downloaded. cd vim

Know your options

Vim comes with a lot of option, and you should learn to pick and choose which you need.

Me personally, always start with the vanilla compile. When you discover a requirement, sudo make uninstall, make clean, hg pull (mercurial update), .configure --with new options, make, sudo make install.

./configure --help

Will list all available options to you. Got it? Moving on.

Quick note: If you have a 4 core processor, you can do make -j8. If you have a 1 core processor you can do make -j2. Which will multi-thread your make command by the argument.

Then you can simply sudo make install and vim, vi will work again. And you’ll be using the latest build

You gained 50 experience.

Composing your own .vimrc file

Before today, I had no idea how to write my own .vimrc file, figure it out on my own, how it works, etc.

But after running help :vimrc, all of that changed. Here’s the snippet in particular:

 c. Four places are searched for initializations.  The first that exists
    is used, the others are ignored.  The $MYVIMRC environment variable is
    set to the file that was first found, unless $MYVIMRC was already set
    and when using VIMINIT.
    -  The environment variable VIMINIT (see also |compatible-default|) (*)
       The value of $VIMINIT is used as an Ex command line.
    -  The user vimrc file(s):
                "$HOME/.vimrc"      (for Unix and OS/2) (*)

osse on #vim @ freenode was kind enough to tell me about nocompatible mode(:help nocomp) which makes it a very ideal thing to put at the very top of your .vimrc in my beginner’s opinion.

When you’ve compiled vim. You’ll notice when you hit tab. It produces ^I. If you’re smart you’ll overcome this by doing shift+tab. If you’re even smarter, you’ll add set nocp to the .vimrc. But don’t do it until you read the help definition otherwise it decreases what you’ll learn from this.

I’m also experiencing that I can’t erase something with backspace.

:set will return all current options. Which shows that nothing appears to be messing with my backspace.

After poking around the docs :help bs seems to have saved me. Adding what I needed to my .vimrc I can proceed.

You gained 50 experience

You are now level 2!

Text

Get rid of code in your Cucumber features

Hi everyone! Just wanted to show you some powerful code with Ruby.

Feature: Dashboard
  In order to have a good overview of the site
  As a user
  I want a dashboard

  Scenario: Dashboard shows the latest tips
    Given the following tips exist:
      | game_title         | tip                                             | tag_list         |
      | Starcraft 2        | Always make SCVs                                | basics, economy  |
      | World of Warcraft  | Questing is the fastest way to gain experience  | basics, leveling |
      | Grand Theft Auto 4 | Armor is a good way to keep you alive           | basics           |
    And I am signed in
    And I am on the dashboard
    Then I should see these tips in order:
      | Game               | Tip                                             | Tags             |
      | Grand Theft Auto 4 | Armor is a good way to keep you alive           | basics           |
      | World of Warcraft  | Questing is the fastest way to gain experience  | basics, leveling |
      | Starcraft 2        | Always make SCVs                                | basics, economy  |

Notice how I’m using game_title and tip and tag_list. I’m doing that so I can easily pass those column names as parameters into a model directly.

However that’s not what Cucumber should be.

Cucumber should **not ** be code.

Refactored:

Feature: Dashboard
  In order to have a good overview of the site
  As a user
  I want a dashboard

  Scenario: Dashboard shows the latest tips
    Given the following tips exist:
      | Game Title         | Tip                                             | Tag List         |
      | Starcraft 2        | Always make SCVs                                | basics, economy  |
      | World of Warcraft  | Questing is the fastest way to gain experience  | basics, leveling |
      | Grand Theft Auto 4 | Armor is a good way to keep you alive           | basics           |
    And I am signed in
    And I am on the dashboard
    Then I should see these tips in order:
      | Game               | Tip                                             | Tags             |
      | Grand Theft Auto 4 | Armor is a good way to keep you alive           | basics           |
      | World of Warcraft  | Questing is the fastest way to gain experience  | basics, leveling |
      | Starcraft 2        | Always make SCVs                                | basics, economy  |

Here’s the step that will convert all these column names and insert them into the model.

Given /^the following tips exist:$/ do |table|
  # table is a | Starcraft 2        | Always make SCVs                                | basics, economy  |
  table.hashes.each do |row|
    row = row.map{ |k| {k[0].parameterize('_') => k[1]} }.inject(:merge)
    Factory(:tip, row)
  end
end

The magic is where I map each parameter and get rid of all special characters and have them replaced by underscores. Then I inject merge to merge the array of hashes into one hash again.

Text

Rails 3.1.2 and Ruby 1.9.3p0 released

Christmas is early @rubyonrails!

rvm get && rvm reload && rvm install 1.9.3