Lojic Technologies

Posts Tagged ‘utility

Setup shoulda and rcov for Rails 2.2.2

with 4 comments

I had a few mis-configuration issues when setting up shoulda and rcov for a new Rails 2.2.2 project, so I thought I’d jot down a few notes (mini tutorial, quickstart) to help save others from burning time on what should be a simple task.

shoulda is a library build on Test::Unit that provides helpers, macros and assertions to make testing easier.

rcov is a code coverage tool for Ruby.

1. Install rcov

sudo gem install rcov

2. Install shoulda

sudo gem install thoughtbot-shoulda --source=http://gems.github.com

3. Create your Rails project

rails myapp

4. Modify myapp/Rakefile

require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require 'shoulda/tasks'
namespace :test do
  desc 'Measures test coverage'
  task :coverage do
    rm_f "coverage"
    rm_f "coverage.data"
    rcov = "rcov -Itest --rails --aggregate coverage.data -T -x " rubygems/*,/Library/Ruby/Site/*,gems/*,rcov*""
    system("#{rcov} --no-html test/unit/*_test.rb test/unit/helpers/*_test.rb")
    system("#{rcov} --no-html test/functional/*_test.rb")
    system("#{rcov} --html test/integration/*_test.rb")
    system("open coverage/index.html") if PLATFORM['darwin']
  end
end

5. Modify myapp/test/test_helper.rb

...
# Add the following line
require 'shoulda/rails'   # require 'shoulda' also worked
...

Conclusion
After you’ve written some shoulda tests, you should be able to use the following rake commands:

rake test
rake test:units
rake shoulda:list    # display specs from shoulda tests
rake test:coverage   # run rcov and display code coverage

Written by Brian Adkins

February 26, 2009 at 9:10 pm

Posted in programming

Tagged with , , , , ,

Ubuntu Linux 8.04 – Wake on LAN

with 3 comments

Now that I’ve switched to a Macbook Pro with OSX Leopard as my primary desktop, I’ve located my Ubuntu machine in another part of the house to be accessible to my children. Not wanting to walk to the room where it’s located just to flip the power switch, I researched how to get “wake on LAN” working, so I could power it up remotely.

1. Enable the appropriate setting in your BIOS. Mine had something to do with wake on PCI device.

2. Install ethtool if you don’t already have it.

sudo apt-get install ethtool
cd /etc/init.d
sudo vim wakeonlanconfig

Add the following lines to that file:

#!/bin/bash
ethtool -s eth0 wol g

Install the script:

sudo update-rc.d -f wakeonlanconfig defaults

Run the script:

sudo /etc/init.d/wakeonlanconfig

3. Keep the network interface alive after shut down.

sudo vim /etc/init.d/halt

Change the following line:

halt -d -f -i $poweroff $hddown

to the following line (i.e. remove the -i)

halt -d -f $poweroff $hddown

4. Get the MAC address

ifconfig | grep HW

5. Send the magic packet via the following Ruby program:

require 'socket'
mac_addr = "x21x53x39xB3x90x42"
s = UDPSocket.new
s.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
s.send("xff"*6 + mac_addr*16, Socket::SO_BROADCAST, '10.0.0.255', 7)

Written by Brian Adkins

September 3, 2008 at 12:23 am

Automatically Delete Unwanted Cookies in Firefox

with one comment

I prefer to not have cookies stored in my browser, but it’s impractical to not store any cookies since this would require repeatedly logging in to authenticated sites that I frequently use. A simple solution in Firefox is the following:

From the Edit menu, choose Preferences and then click the Privacy tab. You should see a dialog similar to the following one:

firefox1.png

Check the “Accept cookies from sites” checkbox. For the “Keep until” setting, select “I close Firefox”. The latter is the key – it will erase all cookies from Firefox whenever you close the program. Of course, we don’t want to erase all the cookies, so click the “Exceptions…” button on the right and you’ll see a dialog similar to the following:

firefox2.png

Just type the name of the web site you want to allow in the text box and click the “Allow” button, and Firefox will add it to the exception list so it won’t be deleted when you close Firefox. You can add a full URL such as http://www.MySite.com, or just the domain name MySite.com to allow cookies for any host in that domain. You an also add sites you want to disallow any cookies from by clicking the “Block” button.

I have about 30 sites that I allow Firefox to store cookies for, but this technique has helped me avoid accumulating tons of unwanted cookies in Firefox. I hope it’s helpful for you.

Written by Brian Adkins

January 26, 2008 at 1:06 pm

Posted in internet

Tagged with , , ,

Use vimdiff to display subversion diffs

with 3 comments

I prefer using vimdiff or gvimdiff to view differences between files. When researching ways to allow using vimdiff to view subversion differences, I came across this article.

The bottom line is that subversion passes the two relevant arguments as the 6th and 7th arguments, so the following shell script wrapper does the trick:

#!/bin/sh
/usr/bin/gvimdiff ${6} ${7}

Save the script as gvimdiff_wrapper.sh, make it executable and accessible on your path. Then modify $HOME/.subversion/config to have the following line:

diff-cmd = gvimdiff_wrapper.sh

That will allow you to use gvimdiff to display the diff generated by svn diff my_file.txt

Written by Brian Adkins

November 27, 2007 at 10:14 am

Posted in programming

Tagged with , , ,

del.icio.us Tag Bundling

leave a comment »

I’ve written about del.icio.us several times before (use the search box to find the articles). I’ve been using the service for quite a while and still consider it to be one of the most valuable web services I use.

I just discovered the tag bundling feature from this article and tried it out. Tag bundling, as you might expect, allows you to group your tags. For example, my first bundle was “people”, so now I can see all my people tags in one group. I’ll be adding more bundles soon.

If you’re not using del.icio.us, you should really check it out. And if you, are and don’t know about tag bundling, give it a shot.

del.icio.us makes it easy to share tags – for example, here’s a link for my bookmarks on the Ruby programming language. I haven’t discovered a similar way for sharing bundles, so if you know, please leave a comment.

Written by Brian Adkins

November 3, 2007 at 8:47 pm

Posted in internet

Tagged with ,

Greasemonkey Script for Netflix Half Star Ratings

with 3 comments

I wrote an article back in May about a way to give half star ratings on Netflix. It had the advantage of working in any browser and not requiring any software installation, but it wasn’t very user friendly.

Since then, I’ve been doing a lot of JavaScript coding, so I thought I’d give Greasemonkey a try. I found a script here to give half-star ratings, but I didn’t care for the hover captions and JSLint pointed out a few issues, so I cleaned it up a little:

Code

// ==UserScript==
// @name Netflix Half Stars
// @description allows half star user ratings on Netflix
// @include http://*netflix.com/*
// ==/UserScript==
// http://userscripts.org/scripts/review/8118
// Modified by Brian Adkins

if (!unsafeWindow.sbHandler) { return; }

var sbHandler = unsafeWindow.sbHandler;
sbHandler.sbOffsets = [8,18,27,37,46,56,65,75,84,94];

sbHandler.displayStrings[0.5] = ".5 stars";
sbHandler.displayStrings[1.5] = "1.5 stars";
sbHandler.displayStrings[2.5] = "2.5 stars";
sbHandler.displayStrings[3.5] = "3.5 stars";
sbHandler.displayStrings[4.5] = "4.5 stars";

sbHandler.sbImages[0.5] = new Image();
sbHandler.sbImages[0.5].src = sbHandler.imageRoot+"stars_2_5.gif";

for(var i = 2; i < 11; i++) {
sbHandler.sbImages[i/2] = new Image();
sbHandler.sbImages[i/2].src = sbHandler.imageRoot + "stars_2_" +
(Math.floor(i/2)) + (i % 2 === 0 ? "0" : "5") + ".gif";
}

sbHandler.getStarCount = function (evt) {
var x = unsafeWindow.getElementMouseCoordinate(evt, this.element);

for(var ii = 0; ii < 10; ii++) {
if(x <= this.sbOffsets[ii]) { return (ii + 1) / 2; }
}

return 0;
};

Installation

Save the JavaScript code with .user.js extension e.g. netflix_halfstar.user.js and then open that file in Firefox and Greasemonkey should prompt you to install it.

Written by Brian Adkins

August 27, 2007 at 4:38 pm

Social Bookmarking

leave a comment »

Here’s a video that explains why using a site such as del.icio.us can be useful. I think they may have failed to mention that you can mark bookmarks as private on del.icio.us, so it’s not necessary to expose your bookmarks to the world. However, in my case, I only mark a small fraction as private.

I’ve been using del.icio.us for quite some time. After I had been using it for a while, I realized that it had been a long time since I bookmarked something in my browser because I had developed a habit of bookmarking in del.icio.us. Most browsers force you into placing a bookmark into a hierarchical, or directory, structure, but on del.icio.us you can assign as many “tags” as you like to a particular bookmark so you can search for things more easily. del.icio.us also allows you to export your bookmarks so you aren’t at the mercy of a proprietary service.

Another thing that is handy is to subscribe to the del.icio.us feeds of your friends to be automatically notified when they bookmark something that may be of interest.

Written by Brian Adkins

August 7, 2007 at 8:34 pm

Posted in internet, video

Tagged with , ,