2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Using Metaprogramming to refactor code

Last updated at Posted at 2014-09-11

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
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?