Thursday, February 14, 2008

A simple DRY

It's interesting a lot of people come to like how ROR organized directories eg app, lib, public, db, etc. The point of attack seems to be homogeneous but nevertheless very handy when bundling particular category.

I come to like it too a lot specially expecting the controllers, model can concentrate on its functionalities rather than cluttering the classes/modules with bits and pieces of delegate methods.

Below is a simple DRY (don't repeat yourself) implementation of a template pattern inside ApplicationController (class require's are not explicitly shown). The main intent is to have one entry call for getting a util class. The redundant other implementation would require if-else statements to extract the Util class per controller.

class ApplicationController
...
def user_template
util = get_util #template_method
util._method1
...
end

...
def get_util #template_method
end
...
end

class AController < ApplicationController
def get_util
UtilA
end
end

class BController < ApplicationController
def get_util
UtilB
end
end

# lib/ folder classes

class Util
def self._method1
end
end

class AUtil < Util
def self._method1
#customized implementation here
end
end

class BUtil < Util
def self._method1
#customized implementation here
end
end

Why Bill Gates is Reckoned to be the Greatest Gift to Secular Humanity

No comments: