Some more ActiveRecord bashing

This time, it took me around 8 working hours since the start of a new ruby-based project before I stumbled upon what seems to be a serious bug. The first time I tried to extend an association, activerecord failed with
associations.rb:1368:in `const_set': wrong constant name FooBar::FooBarsAssociationExtension (NameError)
which can be easily reproduced running the following code: {syntaxhighlighter brush:ruby} require 'rubygems' require 'activerecord' module FooBar class Foo

ActiveRecord and the DRY3 principle

ActiveRecord is a beautiful piece of software. But just step aside of the prescribed road, and you are guaranteed to have problems.

This time, my problems began when I started to design the model first. I quickly laid out a couple of screens of objects, wrote the connection line and ran the code.

class SubjectLocator  "String"
end

# a dozen more objects follow...

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

It has not come as a surprise that ruby replied with an exception.

Table 'activerecord.subject_locator' doesn't exist: SHOW FIELDS FROM `subject_locator` (ActiveRecord::StatementInvalid)

What surprised me is that after half an hour of search on the internet, I found no way to boostrap a DB schema out of the model. There were discussions on the rails mailing list. They even went as far as suggesting non-destructive automatic migrations, a feature that exists in Hibernate for at least 5 years already. None dared to implement either.

I am now left with one solution. Apply the DRY3 principle, aka "Do Repeat Yourself 3.times" and start writing the migrations script for my models (repeat!), as well as an SQL schema for my MySQL database that would keep indexes, views and triggers in addition to what I will boostrap from migrations (repeat again!)

Авторское право vs. всенародное обсуждение

Тут Лукьяненко поставил ссылку на высказывание об авторском праве Романа Афанасьева. Что симптоматично, у обоих комментарии отключены. К чему бы это?

ЗЫ: Назовите меня идиотом, но чтобы найти ссылку на этот постинг Лукьяненко, пришлось прошерстить 431 страничку, от id предыдущего постинга до id следующего. Примерно вот так:

for i in `seq 304622 305053`; do
wget http://doctor-livsy.livejournal.com/$i.html
done |grep gringrin *html
Интересно, есть ли какой-нибудь более прямой способ ставить линки на постинги с отключенными комментариями?
Tags: 

rubygems as the biggest obstacle to Ruby's adoption

Everyone and his friend is already aware that installing rubygems to a non-default location was a nightmare just until very recently (that is, version 0.9.5, produced on 2007-11-19).

Now, suppose we try to do something simpler, like err... uninstalling a gem. gem remove mygem yields no results. I do gem --help and... there is nothing about removing a gem in the command line help. Fine. Before deleting manually, let's look it up on the internet. RubyGems User Guide says it should be gem uninstall mygem. How nice!

OLPC: Omnipresent Loyalty Protection & Control

Тут abbra пишет про удачное внедрение OLPC и ненароком сообщает статистику испльзования этих компьютеров детьми. Это значит, что кто-то, даже не родители, а в лучшем случае учителя мониторят детские компьютеры на предмет того, что и когда они написали. А вы бы хотели, чтобы в ваш комп заглядывали без спросу?
Tags: 

Looks like it is not limited to Belgium

There is a whole thread on /. about the pay system failures in US public schools.

As usual with slashdot, you get sometimes a pearl while digging through a multitude of clueless comments. Here is one I found out for you:

Over time you get more and more complex rules for how to calculate pay. The end result is that nobody understands their pay slips anymore and it is nigh impossible to implement and test a system that handles all the exceptional cases.

Groovy documentation and the human mind

How can one not be disappointed with Groovy after looking at its User Guide?

Here is just one example. Can you tell from the StreamingMarkupBuilder documentation how it works? I bet no. After looking through the code snippets on the page and figuring out I could not understand a line of code there, I wondered what is the mkp variable that appears twice on the page.

It took me some time to get through the Groovy class hierarchy up to the BaseMarkupBuilder.java just to find the mkp namespace declaration.

 namespaces.put("mkp", "http://www.codehaus.org/Groovy/markup/keywords");   // pseudo namespace for markup keywords

After googling for mkp markup keywords, I found a page that explains in a few words the design of markup builders. Nothing more, nothing less. Now, I know everything I had to know about mkp and markup builders. The thing is, it is a post of the developer who just committed the markup builders feature. The documentation is supposed to be better that that but it not even worse, it is blatantly misleading.

Controlling the size of the $PWD in bash, version 2

Er... after reading the insightful comments at debian-administration.org, I figured out a much shorter version which is also easier to understand. Tested on GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu).

{syntaxhighlighter brush: bash} function truncate_pwd { newPWD="${PWD/#$HOME/~}" local pwdmaxlen=$((${COLUMNS:-80}/4)) if [ ${#newPWD} -gt $pwdmaxlen ] then newPWD=".+${newPWD: -$pwdmaxlen}" fi } PROMPT_COMMAND=truncate_pwd PS1="${ttyname}@\h:\${newPWD}\\$ " {/syntaxhighlighter}

Controlling the size of the $PWD in bash

I just fixed a 6-year old bug in the 5 lines of code that I use at least 100 times a day, every day, including weekends and holidays. Now, I can finally publish it.

function truncate_pwd
{
 if [ $HOME == $PWD ]
 then
   newPWD="~"
 elif [ $HOME ==  ${PWD:0:${#HOME}} ]
 then
   newPWD="~${PWD:${#HOME}}"
 else
   newPWD=$PWD
 fi

  local pwdmaxlen=15
  if [ ${#newPWD} -gt $pwdmaxlen ]
  then
    local pwdoffset=$(( ${#newPWD} - $pwdmaxlen  ))
    newPWD=".+${newPWD:$pwdoffset:$pwdmaxlen}"
  fi
}

PROMPT_COMMAND=truncate_pwd
PS1="${ttyname}@\[${HOST_COLOUR}\]\h\[${RESET_COLOR}\]:\${newPWD}\\$ "

Just put the above snippet of code into ~/.bashrc

Debian shows the absolute path in the command prompt by default, and it can be really long, sometimes.To fix this, we can limit the command prompt to show only the last x characters using only the variable expansion features of bash 2.0.5+. Since this code does not fork out sed, tr or wc, it is blazingly fast.

Pages