Archive for the 'English posts' Category

Ruby recipie: extending methods without loose the original code

Supose you want to extend the method of a class called Attachment.

# original_file.rb

class Attachment
  def method_to_extend
    puts "The original code"

If we try to rewrite the method, older code gets lost, but instead we want it to be executed with our code extension

We want to achieve something like the following:

class Attachment
  def method_to_extend
    puts "The original code"
    puts "My extension code!"

But the original code is part of a library we cannot change directly.

The best way I found is to write an alias_method with the original method code and rewrite the method adding the desired code

# extension_file.rb

require 'original_file'
class Attachment
  alias_method :source_method_to_extend, :method_to_extend
  def method_to_extend
    puts "My extension code!"

Building subversion on Leopard

Before anything you have to install X Code Tools you can find in Os X DVD.This tutorial has been done with Leopard, but it should work in Tiger as well.

Downloading files

Download berkeley DB source from oracle site


Download apache portable runtime source with utils and iconv


Download subversion source


Building sources

Building berkeley DB

Unpack source and build source

  $ tar xfz db-4.6.21.tar.gz
  $ cd db-4.6.21/build_unix
  $ ../dist/configure --prefix /opt/svn && make
  $ make install

Building apache portable runtime with utils and iconv

Unpack apr and build source

  $ tar jxf apr-1.2.12.tar.bz2.tar$ cd apr-1.2.12
  $ ./configure --prefix=/opt/svn && make
  $ make install

Unpack apr-utils and build source

  $ tar jxf apr-util-1.2.12.tar.bz2.tar
  $ cd apr-util-1.2.12
  $ ./configure --prefix=/opt/svn \    
                --with-berkeley-db=/opt/svn \    
                --with-apr=/opt/svn && make
  $ make install

Unpack apr-iconv and build source

  $ tar jxf apr-iconv-1.2.1.tar.bz2.tar
  $ cd apr-iconv-1.2.1
  $ ./configure --prefix=/opt/svn --with-apr=/opt/svn && make
  $ make install

Building subversion

Unpack subversion and build source

  $ tar jxf subversion-1.4.6.tar.bz2
  $ cd subversion-1.4.6
  $ ./configure --prefix=/opt/svn \
                --with-berkeley-db=/opt/svn \
                --with-apr=/opt/svn \
                --with-apr-util=/opt/svn \
                --with-ssl && make
  $ make install

Final configuration

Finally we create a repository for our project doing something like that:…but first would be a good idea to add the new path to our profile:

  $ echo "export PATH=/opt/svn/bin:$PATH" >> ~/.profile
  $ export PATH=/opt/svn/bin:$PATH

Initialize the repository

  $ mkdir -p /path/to/new/repository
  $ svnadmin create /path/to/new/repository
  $ svn import /path/to/local/files \    
               file:///path/to/new/repository/project_name \     
               -m "Comment of the initialitzation"

Once the repository is created then we can access to it with a gui program such as svnX. The first time you open the svnX it shows you an error because it can’t find svn commands. Properties window will open, you only have to set this path: /opt/svn/bin
Window properties
Then add the repository to repositories window like this:
Now double click to the repository and click to “svn checkout” choose the route where you want the local copy…
Do checkout
…and it will be added to working copies
Working copies
And that’s all you need to start working with subversion.

Javascript forms validation

This verification method consists in a verification function whose default value is true unless one or more validations change his value.

This function is called from the onsubmit event of the form tag, so the form won’t be submited unless all validations are “valid”. Look at this example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <title>Prova Formulari</title>
       Here is the form validation library we use in the function above. 
    <script src="javascript/formularis.js" type="text/javascript"></script>
    <script type="text/javascript">
      //This function will be called all times that our form is submited.
      //Before any information is send this validations must pass. By
      //doing it this way we ensure that the form is allways validated,
      //nonetheless if you verify information in the server site it’s
      //better  though.
      function verify(){
        //If textbox with id nom is empty, then label with id lnom will
        //be printed red. As you can see I use a naming convention
        //to ease the development. In other words if it’s not valid
        //ret=false and our form won’t be submit until all of them pass.          
        //This mail function validates format and emptiness          
        //That function validates that accept checkbox is marked          
        //In case of this field is optional, but we want ensure
        //it’s correct format("num" or “mail"). Last argument
        //are optional characters we want to include. For example “.-"          
        //To validate numeric fields          
        //If we want our users to choose passwords with a minimum
        //size and ensure they’ve typed it well.          
        //If we want to validate some option is chosen in a combo or a radio.
        //Default option must contain empty value in combo.          
        return ret;
    <!-- verify() function is called at onsubmit event here in the form. -->
    <form action="valida.php" onsubmit="return verify()" method="post">
         Look! Each element we validate has a label tag asociated. Label id is
         named like the element with an "L" at the begining.
      <label id="lnom" for="nom">Name:
        <input type="text" id="nom" name="nom" />
      <br />
      <label id="lmail" for="mail">E-mail:
        <input type="text" id="mail" name="mail" />
      <br />
      <label id="laccept" for="accept">
        <input type="checkbox" id="accept" name="accept" />
        I have read, understood and I accept all terms
      <br />
      <label id="ltel" for="tel">Telephone(optional):
        <input type="text" id="tel" name="tel" />
      <br />
      <label id="lweight" for="weight">Weight:(optional)
        <input type="text" id="weight" name="weight" />
      <br />
      <label id="lpassword" for="password">
        <input type="password" id="password" name="password" />
      <br />
      <label id="lrepassword" for="repassword">
        <input type="password" id="repassword" />
      <br />
      <label id="lcolors" for="colors">
        Favourite color:
        <select id="colors" name="colors">
          <option value="">Choose a color</option>
          <option value="red">red</option>
          <option value="green">green</option>
          <option value="blue">blue</option>
      <br />
      <label id="lanswer" for="answer">
        Do you like cheese?
      <input type="radio" value="yes" name="answer" id="answer" />yes
      <input type="radio" value="no" name="answer" id="answer"  />no
      <br />
      <input type="submit" value="Send" />

Look this example in the following link: validation form example

Download source files from here.