Tuesday, April 27, 2010

hirb

After jumping from link to link i came across hirb.  What i found most useful about it is that it formats ActiveRecord descendants to a table. no more y for me!

Install:

sudo gem install hirb

@console:
>> require 'hirb'
>> []
>> Hirb.enable
>> true
To disable:
>> Hirb.disable
>> false
Sample output for ActiveRecord object
  # Tag is a model class and descendant of ActiveRecord::Base
  >> Tag.last
  +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
  | id  | created_at              | description | name          | namespace | predicate | value |
  +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
  | 907 | 2009-03-06 21:10:41 UTC |             | gem:tags=yaml | gem       | tags      | yaml  |
  +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
  1 row in set
  >> tags = Tag.all :limit=>3, :order=>"id DESC"
  +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
  | id  | created_at              | description | name              | namespace | predicate | value    |
  +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
  | 907 | 2009-03-06 21:10:41 UTC |             | gem:tags=yaml     | gem       | tags      | yaml     |
  | 906 | 2009-03-06 08:47:04 UTC |             | gem:tags=nomonkey | gem       | tags      | nomonkey |
  | 905 | 2009-03-04 00:30:10 UTC |             | article:tags=ruby | article   | tags      | ruby     |
  +-----+-------------------------+-------------+-------------------+-----------+-----------+----------+
  3 rows in set
  

To make your own tables while in console, you could also use the table() and view() methods by
extend Hirb::Console
For example you only want to view cerain fields:
  >> table tags, :fields=>[:id, :description, :name]
  +-----+-------------+-------------------+
  | id  | description | name              |
  +-----+-------------+-------------------+
  | 907 |             | gem:tags=yaml     |
  | 906 |             | gem:tags=nomonkey |
  | 905 |             | article:tags=ruby |
  +-----+-------------+-------------------+
  3 rows in set
Or:  
  # Create a table of Dates comparing them with different formats.
  >> table [Date.today, Date.today.next_month], :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
  +------------+--------+-----------+-------+--------------------------+
  | to_s       | ld     | ajd       | amjd  | asctime                  |
  +------------+--------+-----------+-------+--------------------------+
  | 2009-03-11 | 155742 | 4909803/2 | 54901 | Wed Mar 11 00:00:00 2009 |
  | 2009-04-11 | 155773 | 4909865/2 | 54932 | Sat Apr 11 00:00:00 2009 |
  +------------+--------+-----------+-------+--------------------------+
  2 rows in set
I placed this in my .irbrc file:
require 'hirb'
Hirb.enable
extend Hirb::Console 
Coolness! hirb's README says it all, go check it out. 

Wednesday, April 21, 2010

Who skips?

Callbacks skipped by:
 - delete
 - delete_all

Validations skipped by:
 - update_attribute
 - save(false)

Wednesday, April 7, 2010

Rspec Matchers you'll usually use

should ==   # there's also should eql and should equal which compares only the values (not the identity)
should_not ==
should be_nil
should_not be_nil
should be_true
should_not be_true
should be_empty
should_not be_empty
should be_valid
should_not be_validshould include


Always use should_not rather than negating whatever follows it.  And it's also good practice to have one expectation per example (with exceptions, i think). Isn't testing tedious?

To run tests automatically, use autospec.

Pilot

A rails blog is born!

This is pretty much just my online notes to remind me of things. But I do hope it also helps someone else out there.