Defining Class methods in a Module

By kenglish

The code should speak for itself. Make sense?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Loveable
  module ClassMethods
    def give_hug
    end
  end
  def self.included(base)
    base.extend(ClassMethods)
  end
end
 
class Person
  include Loveable
 
  give_hug
 
end

I fuzzy as to why a certain Rails genius would suggest it is better to do it this way
(see line 7):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Loveable
  module ClassMethods
    def give_hug
    end
  end
  def self.included(base)
    base.send :extend, ClassMethods
  end
end
 
class Person
  include Loveable
 
  give_hug
 
end

Feel free to comment…

categoriaProgramming commentoNo Comments dataNovember 20th, 2009
Read All

Install thoughtbot shoulda and rcov (the right way)

By kenglish

Install rcov & ruby-prof (rcov-0.9.6 & ruby-prof-0.7.3 at the time of this writing).

sudo gem install ruby-prof rcov --no-ri --no-rdoc

Update your test/test_helper.rb, add:

require 'shoulda/rails'

Install Thoughbot’s Shoulda gem (shoulda-2.10.2 at the time of this writing). Make sure you have added GemCutter as one of your ruby gem sources.

sudo gem install shoulda --no-ri --no-rdoc

Edit your applicaitons main Rakefile and add:

require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require 'shoulda/tasks'
 
def run_coverage(files)
  rm_f "coverage"
  rm_f "coverage.data"
 
  # turn the files we want to run into a  string
  if files.length == 0
    puts "No files were specified for testing"
    return
  end
 
  files = files.join(" ")
 
  if PLATFORM =~ /darwin/
    exclude = '--exclude "gems/*"'
  else
    exclude = '--exclude "rubygems/*"'
  end
 
  rcov = "rcov --rails -Ilib:test --sort coverage --text-report #{exclude}  --aggregate coverage.data"
  cmd = "#{rcov} #{files}"
  puts cmd
  sh cmd
end
namespace :test do
 
  desc "Measures unit, functional, and integration test coverage"
  task :coverage do
    run_coverage Dir["test/**/*.rb"]
  end
 
  namespace :coverage do
    desc "Runs coverage on unit tests"
    task :units do
      run_coverage Dir["test/unit/**/*.rb"]
    end
    desc "Runs coverage on functional tests"
    task :functionals do
      run_coverage Dir["test/functional/**/*.rb"]
    end
    desc "Runs coverage on integration tests"
    task :integration do
      run_coverage Dir["test/integration/**/*.rb"]
    end
  end
end

Checkout your new coverage rake tasks:

rake -T | grep cov

Should show you:

rake test:coverage                        # Measures unit, functional, and integration test coverage
rake test:coverage:functionals            # Runs coverage on functional tests
rake test:coverage:integration            # Runs coverage on integration tests
rake test:coverage:units                  # Runs coverage on unit tests

categoriaProgramming commentoNo Comments dataNovember 19th, 2009
Read All

Example Bash script to rename directories

By kenglish

This script will rename directories based on a pattern. My goal was to rename my directories so the album year would be in [] and not (). For example, we would move /home/Music/ACDC/Back in Black (1980) to /home/Music/ACDC/Back in Black [1980]. The downside is you have to enter the pattern twice, once for bash and once for sed.

#!/bin/bash
 
find  /home/kenglish/Music -type d | while read DIR; do
 
  if [[ "$DIR" =~ \([0-9]{4}\)$ ]]; then
    NEW_DIR=`echo $DIR | sed 's/(\([0-9][0-9][0-9][0-9]\))$/[\1]/'`
    echo "$DIR    -->    $NEW_DIR" 
    ` mv "$DIR" "$NEW_DIR" `
  fi
done

categoriaLinux commentoNo Comments dataNovember 17th, 2009
Read All

Already some haters of Google Closure

By kenglish

I saw this article on Sitepoint about Google Closure: How not to write JavaScript. The author claims that Closure is just Java programmers trying to make Javascript like Java. Having spent a lot of time doing ExtJS over the past few months, I’ve grown rather fond of Javascript. I would say the worst part about it is the scoping problems.

categoriaProgramming commentoNo Comments dataNovember 15th, 2009
Read All

Bash script to copy files in order to my Coby mp305

By kenglish

I’m one of those people that refuses to get an IPOD. I think they are too expensive and they don’t play nice with Linux.

Last Christmas, I bought myself the 4GB Coby mp305 because it has more capacity than the Sandisk Sansa m200. The interface is crap compared to the Sansa m200. It doesn’t read the mp3 ID3tags at all. The navigation tree is simply the directory structure.

The major flaw is that it does not always sort files in the directory in the correct order. I finally figured out that it sorts files by the order that they were put on the device. However, for some reason in linux if you do “cp -R”, it doesn’t put them on in the proper order.

Here’s my script to put files on the device, it’s call coby_copy.sh:

#!/bin/bash
 
if [ !-d $1 ]; then
   echo "Source Directory does not exists"
   exit
fi
 
if [ !-d $2 ]; then
   echo "Target Directory does not exists"
   exit
fi
 
echo "arg1 = $1 arg2 = $2"
 
IFS=`echo -en "\n\b"`
 
for FILENAME in `find $1 -type f -iname "*mp3" -print | sort | sed 's/^\.\///'` 
do
  DIR=`dirname $FILENAME`
  mkdir -p $2/$DIR
  echo $FILENAME
  cp $FILENAME "$2/$DIR"
done

To run it:

coby_copy.sh "Harry Potter and Leopard-Walk-Up-to-Dragon"  "/mnt/disk/Audio Books"

categoriaLinux, Programming commentoNo Comments dataAugust 18th, 2009
Read All

MsSql: Select table column names

By kenglish

Sometimes I need to match table column names in Microsoft SQL Server. This seems to be the best way to do it:

SELECT COLUMN_NAME, TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%COST%'

categoriaProgramming commentoNo Comments dataAugust 18th, 2009
Read All

Install ruby gem libxml-ruby on Ubuntu 9.04 (Jaunty)

By kenglish

Quick note on how to install the libxml-ruby gem on Ubuntu:

sudo apt-get install libxml2 libxml2-dev
sudo gem install libxml-ruby

categoriaLinux, Programming commentoNo Comments dataAugust 18th, 2009
Read All

Setup for CAC card on Linux Ubuntu 9.04 in Firefox

By kenglish

Install CoolKey

sudo apt-get install coolkey -y

Install PCSC Tools:

sudo apt-get install libpcsclite1 pcsc-tools pcscd -y

Install dod-configuration-1.0.2.xpi into firefox by downloading it to a local directory and then doing “File|Open”. You have to get this from https://software.forge.mil which you can only get to with your CAC. Yes, I know. This makes a lot of sense. I got it from a co-worker.

Plugin your CAC card reader and insert your CAC. The green light should come on. Next, you will to set up the CAC in Firefox.

Open Firefox, goto Edit | Preferences | Advanced | Security Devices.

Click Load.

Enter the following:
Module Name : CAC Module
Module filename : /usr/lib/pkcs11/libcoolkeypk11.so

Load Device - Cac Module

The CAC should appear in your list of security devices now. Click “Log in” to test it. It should look something like this:

Device-Manager
Click “Log In” and enter your pin to test it.

Try to bring up https://software.forge.mil. Firefox will complain about the cert so just add an exception for it.

You should be all good. Let me know how this works out.

NOTE: I’m using Firefox 3.5

categoriaLinux commentoNo Comments dataJuly 30th, 2009
Read All

What makes a really good Ruby IDE?

By kenglish

Chad Woolley over at pivotal has a blog entry about The Great Ruby IDE Smackdown of ‘09. He compares the IDE’s by doing a task that no Rails developer will ever need to do. What do we really do all day: Model, View, Controller, Test, routes, etc. The IDE should provide an easy way to switch between these. Netbeans does this. Aptana does this. RudyMine does this. They are all functional and when used properly, very effecient. What really matter to me? VI intergration. Netbeans has this with jVi. I love it.

I got a kick out of this:

“To me, the benefits of a memory- and processor-sucking IDE with tons of unnecessary, unconfigurable, resource-eating tiny-ass-fonts and chrome did not justify giving up the speed and responsiveness of a great text editor.”

Memory- and processor-sucking IDE? Is he running a 486dx? Are Macs really that slow? Dude, switch to linux! Or, here’s 10 Reasons You Should Not Switch To Linux.

Here’s another nice feature of NetBeans that your Text Editor won’t do. Notice on line 127, I have a mispelling of the word worksheet. Netbeans bolds the misspelled varialbe to tell me that I have a variable here that has never been used before. This is very helpful.

NetBeans-coolness

categoriaProgramming commentoNo Comments dataJuly 16th, 2009
Read All

Bedazzle Your Bash Prompt with Git Info (for Ubuntu 9.04)

By kenglish

This is in response to the railstip.org post about how to Bedazzle Your Bash Prompt with Git Info. If you do this in Ubuntu, it will screw up your gnome-terminal title.

This is how add the git branch to your prompt in Ubuntu 9.04. Edit /home/kenglish/.bashrc. Find the lines with PS1. Replace them with this:

function parse_git_branch {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"
}
 
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] $(parse_git_branch) \$ '
    #PS1="\w \$(parse_git_branch)\$ "
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $(parse_git_branch)\$ '
fi

categoriaLinux commentoNo Comments dataJuly 1st, 2009
Read All