Wednesday, December 9, 2009

My shortcut to open any gem in TextMate with 1 command

Using the implementation from @peepcode's blog on shell method missing as a base.
I've added another action (code snippet below) to open a gem library in TextMate by typing in the name of the gem followed by ".mate".

when /^[A-Za-z0-9_\-\/]+\.mate$/
  # Open the gem in textmate
  # @example
  #   haml.mate
  gem_to_open = command.first.gsub(/\.mate$/, '')
  run "gem which #{gem_to_open} | tail -1 | xargs dirname | sed -e's/$/\\/../' | xargs mate"

Wednesday, November 25, 2009

Download sources jar from maven

We use Maven, when Maven downloads dependencies it doesn't download sources, which I rely on when learn and debug programs....
I was expecting, for what seem a simple thing to me, a flag that I can add in maven's settings files or an option to pass to mvn command.... but I couldn't find the solution (or anything close to it). Don't get me wrong, there are solutions out there, but I just feel it is too much hassle for the task.

So I wrote a little script that go thru my repo and downloads any jar that doesn't have its respective sources :p

Monday, November 23, 2009

ImageMagick and JPEG on OSX

I am not a fan of port or fink so I always install ImageMagick from source.
Recently I came across a problem where ImageMagick fails to identify JPEG's.
The error looks something like this
identify: no decode delegate for this image format

After some digging around it appears I need to install libjpeg and reinstall ImageMagick for JPEG support.
So I downloaded jpegsrc.v7.tar.gz and just run thru the standard configure, make & make install then reinstalled ImageMagick.
After the installation you should be able to see JPEG as one of the supported format via the command "identify -list format".
It should contain something like this.
JPEG* JPEG rw- Joint Photographic Experts Group JFIF format (70)


Tuesday, September 8, 2009

Mass erb to haml conversion

Do this in your application's root folder.

find . -name "*.erb" | while read f; do html2haml -r $f ${f%.erb}.haml; done;

Thursday, August 27, 2009

How to stop unix read command escape backslashes

If you want to pipe any output that has backslashes in it (or make 'read' take a string with backslashes) to the read command then you will need to stop the read command treating backslash as escape character by using read -r
e.g.
cleartool ls | while read -r file
do
# do something to file
done

Monday, August 24, 2009

Software design or code design

Software design

Generally high level, a.k.a.
  • architecture
  • infrastructure
    e.g. 3-tiers or client server, ejb or not

Code design

  • domain design
  • flow design
  • good code design means clean, maintainable code
  • code design issues are easier to fix than software design
The following extract from Domain Driven Design applies regardless the type of project (waterfall or agile).
Any technical person contributing to the model must spend some
time touching the code, whatever primary role he or she plays on
the project. Anyone responsible for changing code must learn to
express a model through the code. Every developer must be
involved in some level of discussion about the model and have
contact with domain experts. Those who contribute in different
ways must consciously engage those who touch the code in a
dynamic exchange of model ideas through the Ubiquitous
Language.
A solution consultant should touch the code (or at least know the code if they don't want to touch it).

Rails Rumble after thoughts

Building an application in 48 hrs is hard. Time flies when you are not prepared.


Here are some of the things that you want to touch on before the competition starts.

  • application security / access - who has access to what
  • model / resource routes - what models are required and how are they connected from browser / UI perspective
  • fields & columns in models
  • model validation / mandatory fields

For the initial first few models that you add to the application, the effort to add each one does not increases in linear fashion.

Because with each model, you need CRUD pages / functionality. Each additional page needs to be styled therefore more visual design required, also more thoughts required for workflow / navigation design.


Often we have to just implement some crude pages to test / visualise our app (particular for workflow) while our designer work thru the markup, which means we usually have to go back and cleanup those pages' markup to fit with the design.


Overall, it was exhausting but I learnt heaps of cool tricks this year building TechMeets.com.


Monday, August 17, 2009

Using google_ajax_library when offline.

As I mentioned in How do I automatically revert to local javascript when offline?, I eventually forked google_ajax_library gem and added support for working offline.

I sent a pull request to Ryan Heath, the original author the gem and he liked the feature but implemented it slightly differently to what I have done.

How to use WinMerge as ClearCase compare & merge tool?

WinMerge made ClearCase much more bearable to work with.

When installing WinMerge make sure you tick the 'Integrate with ClearCase' option.
That will make ClearCase use WinMerge as the compare program but ClearCase still uses its own merge program when resolving conflicts.
To change that setting you have to modify ClearCase configuration file manually.
The configuration file is usually in the lib managers' folder of the ClearCase installation.
e.g. C:\Program Files\Rational\ClearCase\lib\mgrs\map

The change to the map file is based on what WinMerge did to the xcompare value
;text_file_delta xmerge ..\..\bin\cleardiffmrg.exe
text_file_delta xmerge C:\work\tools\WinMerge\WinMergeU.exe

Tuesday, August 4, 2009

How to validate money amount in ActiveRecord?

This is how I validate money amount in Romey.
Basically an regex that allows up to 2 decimal points or whole integers.

validates_format_of :amount, :with => /^\d+$|^\d+\.\d\d?$/

Wednesday, April 29, 2009

How I installed ruby and run RoR on QNAP TS-219

Set LD_LIBRARY_PATH to be /opt/lib so libssl.so.0.9.8 and libcrypto.so.0.9.8 can be found. Alternatively symlink them to /usr/lib might work as well.


Install ruby & rubygems from source, using default options.


Then run this to install mysql gem.

gem install mysql -- --with-mysql-dir=/usr/local/mysql

Thursday, February 19, 2009

How do I automatically revert to local javascript when offline?

I work on Rails applications that include jquery javascript files from ajax.googleapis.com. Every now and then I have to work on those apps offline.
e.g. when I commute on trains.

The application has the same jquery javascript files locally so the old way of working offline was to comment out the google jquery include calls and uncomment the local javascript include calls.

I decided I didn't want to have to bother with that so I implemented something that will check if I am connected online or not and make the appropriate javascript include calls.

The code looks like this:
- if Ping.pingecho("ajax.googleapis.com", 10, 80)
  = google_jquery
- else
  = javascript_include_tag 'jquery-1.2.6.min.js'


The added bonus is that if ever ajax.googleapis.com goes down then our apps won't be affected.

p.s. the above code is markup in haml and uses google_ajax_libraries_api plugin and ping.rb which comes with standard ruby installation. Down the track I might modify the plugin to support the offline backup.

Monday, January 12, 2009

How to remove windows carriage return (^M) on Mac

I don't want to install dos2unix via port and I know it's just a matter of search and replace those ^M chars.

Here are a couple of command that should do the job of removing those trailing ^M (carriage return) chars.

perl -pi -e's/\r//g' filename
sed -e's/^M//g' filename > newfile

To type ^M in Terminal do Ctrl+V then Ctrl+M.

Friday, January 9, 2009

Use ActiveRecord migration to manage database changes in Java projects

A sort of sequel to dumping schema into ruby. I am going to show how we can use ActiveRecord migration to manage our database change in Java projects.

Problem:
Automatically track which schema change has been made to a database.
Make it easy to recreate another database with the same schema.

Requirements:
bsf.jar (from jruby binary download)
jruby-complete jar
jdbc jar
activerecord gem
activerecord-jdbc-adapter gem

Resources:
http://www.ics.muni.cz/~makub/ruby/#c2

Steps:
install jruby
jruby -S gem install --no-rdoc --no-ri activerecord
jruby -S gem install --no-rdoc --no-ri activerecord-jdbc-adapter


Known issues:

The jruby-complete jar contains rubygems and you can package up the other gems (above) into the jruby-complete jar and distribute it (check it into vcs) that way.

Another way is to set GEM_PATH env variable to the location of the jruby gem directory.
e.g. /Users/andy/dev/tools/jruby/lib/ruby/gems/1.8

At the moment, you need to set GEM_PATH to be the jruby gem directory, which means each machine will require jruby to be installed plus the gems used.
It would be more ideal if we can just repackage jruby-complete jar with the additional gems to make deployment easier.


Update: Thanks to Nick's blog, you no longer have to set GEM_PATH to get the gems required included by the base application.

How to do mass renaming of file extension

Problem:
You want to rename all the .wiki files to be .textile instead.

Solution:
\ls -1 *.wiki | while read f; do mv $f ${f%.wiki}.textile; done

Easy way to backup codebase on USB key with git

Requirements:
git (via cygwin)
$ git --version
git version 1.6.0.4

On USB key
mkdir project.init and cd into it
git --bare init

In project codebase's directory
git init
git add .
git ci -m'first commit'
git remote add origin /path/to/project.git
git push origin master


Modify you local master to track remote by appending the following to your project's .git/config file:
[branch "master"]
remote = origin
merge = refs/heads/master

Then you can just do the following in the future to push new files to your USB.
git add .
git ci -m'update repos'
git push


Gotchas when using git on cygwin

$ git push origin master
Counting objects: 6669, done.
Compressing objects: 100% (6570/6570), done.
Writing objects: 100% (6669/6669), 7.90 MiB | 348 KiB/s, done.
Total 6669 (delta 4488), reused 0 (delta 0)
*** Project description file hasn't been set
error: hooks/update exited with error code 1
error: hook declined to update refs/heads/master
To /cygdrive/e/westpac/compass.git
! [remote rejected] master -> master (hook declined)
error: failed to push some refs to '/cygdrive/e/project.git'


The above problem is caused by a hook script (.git/hooks/update).
Rename update to something like update.bak so it does not get executed solves the above problem.

p.s. with the newer version of cygwin git the hook scripts are all suffixed with .sample in the file name, which means they don't get executed hence won't have the above problem.

Use jruby to dump DB schema to ruby

Problem:

I have a database that rails doesn't have an easy way to connect to.

Solution:

Use jruby & JDBC

Requirements:

jruby
activerecord
activerecord-jdbc-adapter
jdbc driver for the database you want to connect to

Resources:

http://jruby-extras.rubyforge.org/activerecord-jdbc-adapter/

Steps:

jruby -S gem install activerecord
jruby -S gem install activerecord-jdbc-adapter

Put the jdbc jar in Classpath for jruby. e.g. inside C:\work\tools\jruby-1.1.6\lib

Run this script:

require 'rubygems'
gem 'activerecord-jdbc-adapter'
require 'jdbc_adapter'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
:url => 'jdbc:sqlserver://localhost:1433;DatabaseName=FineosSixApp',
:username => 'username',
:password => 'password'
)

ActiveRecord::SchemaDumper.dump


To load the schema into say mysql
require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => 'username',
:password => 'password',
:database => 'database'
)

load('schema.rb')

Future Improvements:

Instead of using jruby on command line, create an ant task that uses jruby.jar and do the same thing.
Basically integrating the schema dump into the build file.

It'll be useful to store the client code base along with schema that runs with the code base.