Extensions for acts_as_tree

August 30th, 2008 No Comments »

While the acts_as_tree rails plugin ( http://dev.rubyonrails.com/svn/rails/plugins/acts_as_tree ) provides some basic methods for accessing tree like data structure, I found that it lacks some basic ones, for example – has_children? and has_siblings?
Other than that, it would have been nice to know the tree width and depth (especially if you need to draw it, and need to calculate the scaling.

module ActiveRecord
  module Acts
    module Tree
      module InstanceMethods
        def has_children?
          !self.children.size.zero?
        end
 
        def has_siblings?
          !self.siblings.size.zero?
        end
 
        # Return the tree depth
        def depth
          return 0 unless has_children?
          children.inject(0) {|dep,c| dep > c.depth ? dep : c.depth} + 1
        end
 
        # Return the tree width
        def width
          return 1 unless has_children?
          children.inject(0) {|sum,c| sum + c.width}
        end
      end
    end
  end
end

Maybe if I won’t be lazy, I’ll write some tests and send this for inclusion in the plugin…

C extension for Porter stemmer

August 20th, 2008 No Comments »

When playing with LSI, I noticed that the program runs for too long and uses enormous amounts of memory.
Using a great tool ruby-prof, I found, to my astonishment, that I waste more time in stemming than in SVD.
So I wanted to try to see, if using a compiled C extension will make a difference. So I took the thread-safe porter algorithm from http://tartarus.org/~martin/PorterStemmer/ and wrapped it with swig.
The results were almost in an order of magnitude (10000 rounds for 11 words):

      user     system      total        real
stem :  3.480000   0.250000   3.730000 (  3.719107)
fstem:  0.440000   0.090000   0.530000 (  0.526526)

This I call “performance boost” :)

porter.i (for swig):

%module stemmer
%{
char *stem_word(char *word)
{
  int length, i;
  char *res;
  struct stemmer * z = create_stemmer();
  length  = stem(z, word, strlen(word)-1);
  /* length is the index of last char, add one for size and one for '�' */
  res = (char *)malloc((length+2) * sizeof(char));
  for (i=0; i<=length; i++)
  {
    res[i] = word[i];
  }
  res[length+1] = 0;
  free_stemmer(z);
  return res;
}
%}
%newobject stem_word;
char *stem_word(char *);

Integrating Rails and dhtmlxTabbar

June 24th, 2008 2 Comments »

The dhtmlxTabbar is a nice tab-bar component from http://www.dhtmlx.com/
In order to use the AJAX loading feature I had to make a small change in the dhtmlxcommon.js so it mimics the behavior of Prototype by adding a X-Requested-With HTTP header. Thus, it’s possible to use request.xhr? in Rails.
dhtmlxcommon.js

Accessing Subversion from Rails in an ActiveRecord way

August 27th, 2007 2 Comments »

I needed to present products and their corresponding branches and tags in Subversion in a Rails app. So I’ve come up with this quite simple solution.

The GenericModel class (attached) is ihnerited by Tag, Branch and Product.
The Product class supports methods find, branches and tags for which it uses ‘svn/client’.

The config.yml (which is read into OpenStruct) is very simple:

svn:
  url: http://svn/svn/repos
  user: roman
  password: roman

The Product class tries to provide interface similar to the ActiveRecord (of course I didn’t immitate everything, just the basic stuff). Implementing interfaces in a contract-less language is an adventure :)

generic_model.rb

product.rb

Bugzilla plugin for Rails

August 14th, 2007 3 Comments »

I finally posted my Bugzilla plugin on Rubyforge.

The project page is:

http://rubyforge.org/projects/rubzilla/

The source can be found here:
svn://rubyforge.org/var/svn/rubzilla/bugzilla/trunk

License:
BSD

It is probably not complete, and has some bugs, and surely lacks documentation, but this is better than nothing :)

Bugzilla 3.0

August 11th, 2007 1 Comment »

Lately I’ve been playing with the Bugzilla 3.0 XML-RPC API.

It turned out to be quite basic and inconsistent, but nevertheless – it’s a progress.

It’s also a CPU hog, so I’d not recommend running Bugzilla without mod_perl (latest versions finally added this long-time awaited feature).

The last time I wanted to integrate Bugzilla in one of our projects, I’d to write a class which mimicked a browser and this way contacted Bugzilla.

But this time I’ve come up with a nice Ruby module for Bugzilla, which has API similar of that of ActiveRecord, making integration of it in Ruby-on-rails application quite simple.

I’ll probably add some comments, throw in some more tests and open a project on rubyforge or something. Not to forget to notify the Bugzilla guys, perhaps they’ll find some use for this code.