?>

Rake stats – adding a custom folder

Rake stats is useful when you need to know the statistics of your project. However if you try to add custom folder to be listed – documentation is somewhat sparse.

Question is why would you need a custom folder? After all isn’t the Rails prescribed way to go… Well not everything is a model or a a lib. You can have complex domain models in Ruby that have nothing to do with the database or the general library utility classes. Those poor classes just scream for the folder of their own.

If you add code to new folder named FancyNewFolder “rake stats” is not picking it up.
Stats before adding custom folder
Here is what I did.

gvim lib/tasks/AppTasks.rake

And then I’ve added

STATS_DIRECTORIES = [
  %w(Controllers        app/controllers),
  %w(Helpers            app/helpers),
  %w(Models             app/models),
  %w(Libraries          lib/),
  %w(FancyNewFolder     app/fancy_new_folder),
  %w(Integration\ tests test/integration),
  %w(Functional\ tests  test/functional),
  %w(Unit\ tests        test/unit)

].collect {|name, dir| [name, "#{RAILS_ROOT}/#{dir}"]}.select {|name, dir| File.directory?(dir)}

desc "Report code statistics (KLOCs, etc) from the application"
task :stats do
  require 'code_statistics'
  CodeStatistics.new(*STATS_DIRECTORIES).to_s
end


VoilĂ  there is your custom folder, I’ll bet the stats are not so good as they used to be, but at least now you know the truth :)
Stats after adding custom folder

Tags: , , , , ,

Redgreen plugin not working with Ruby 1.9.1

Redgreen in action
You have upgraded to Ruby 1.9.1 and now (after manually changing & compiling mongrel to work) you noticed that redgreen plugin is not working. Whenever you start test you get something similar to this:

`require': no such file to load -- test/unit/ui/console/testrunner (MissingSourceFile)

Just type this in:

sudo gem install test-unit -v 1.2.3

If you are one of those “problematic” people that want to know why is this working… truth is much simpler than I would like it to be considering that I lost 2 hours over this :) Ruby 1.9.1 replaced test-unit framework with minitest (because it’s supposed to be better). Redgreen plugin depends on console-runner from test-unit so you need to install it as u gem. (Older version, because the new 2.0.3 changed API just enough so redgreen is not working)

Tags: , , ,

Ruby refactoring fun

I my last post I’ve said that I don’t care if code for method_missing implementation is ugly. Well I’ve lied.
This is starting point

def method_missing(method, *params)
     method = method.to_s
     type = :get
     if (method.index('store_').nil? == false) #if you have name conflict use store_ prefix,
                                                             #or rename hash key or class method
         method.gsub!('store_','')
     elsif (method.index('exists_').nil? == false)
         method = method.gsub!('exsists_','')
         type = :check_if_exists
     elsif (method.index('set_').nil? == false && params.count == 1)
         method = method.gsub!('set_','')
         type = :set
     end
     method = method.to_sym
     if type == :get
         return @store[method] if @store.keys.collect(&:to_sym).include?(method)
     elsif type == :check_if_exists
        return @store.keys.collect(&:to_sym).include?(method)
     else
        return @store[method] = params[0]
     end
     return nil  # return nil if key does not exist
  end

If you want to see what kid of result did I get read on Read the rest of this entry »

Using Ruby Metaprogramming to Increase READABILITY of Code

Let’s assume you have a class that has some sort of hash where it stores it’s data. We want to calculate amount that we need to pay, but only if number of items that we want to sell is entered. We’ll also assume that price is already present so we don’t need to check for that. It’s easy:

@store[:total] = @store[:price] * @store[:amount] if @store.has_key?(:amount)

A bit more readable would be:

set_total(price * amount) if amount_exists

Read the rest of this entry »

Tags: , , ,

Tweak CalendarDateSelect plugin to work with European datetime format

Calendar date select in action

If you want to use calendar_date_select plug-in for date time picker, and you would like to use dd.mm.yyyy format read on!

I wrote this post after many wrong turns and many tutorials that just didn’t work. This informations applies for RAILS 2.3.2, and Calendar Date Select 1.15. I you are anything like me you want proof that my tutorial is working? Read the rest of this entry »

Tags: , , , ,