If you have some methods whose definitons are similar, only different by the method name. You can use meta programming
to simple the things.
Bad code:
class User < ActiveRecord::Base
validate_inclusion_of :activation_state, :in => ["pending", "active"]
def self.pending_users
find :all, :conditions => {:activation_state => 'pending'}
end
def self.active_users
find :all, :conditions => {:activation_state => 'active'}
end
def pending?
self.activation_state == 'pending'
end
def active?
self.activation_state == 'active'
end
end
In this example, we have similar methods definitions of pending_users
, active_users
, pending
, active
. We can simplify the codes by using meta programming
Refactor:
class User < ActiveRecord::Base
STATES = ['pending', 'active']
validate_inclusion_of :activation_state, :in => STATES
class <<self
STATES.each do |state|
define_method "#{state}_users" do
find :all, :conditions => {:activation_state => state}
end
end
end
STATES.each do |state|
define_method "#{state}?" do
self.activation_state == state
end
end
end