次の方法で ActiveRecord のいくつかのメソッドをエイリアスするプラグインを作成しようとしています。
class Foo < ActiveRecord::Base
include MyOwnPlugin
acts_as_my_own_plugin :methods => [:bar]
def bar
puts 'do something'
end
end
プラグイン内:
module MyOwnPlugin
def self.included(base)
base.class_eval do
extend ClassMethods
end
end
module ClassMethods
def acts_as_my_own_plugin(options)
options[:methods].each do |m|
self.class_eval <<-END
alias_method :origin_#{m}, :#{m}
END
end
end
end
end
#acts_as_my_own_plugin が実行されるとき、Foo#bar は実行されていないためまだ定義されていないため、このアプローチは機能しません。
act_as_my_own_plugin :methods => [:bar] を置くと、 bar 関数宣言が機能します。しかし、これはきれいではありません。
ほとんどのプラグインがそうであるように、acts_as_my_own_plugin をクラス定義の上に配置できるようにしたいと考えています。
この条件を満たすための代替アプローチはありますか?