2009年9月2日水曜日

[Rails][CodeReading] Rails::Initializer (36) load_observers

Initializer.process で36番目に呼ばれる load_observers メソッド。

def load_observers
if gems_dependencies_loaded && configuration.frameworks.include?(:active_record)
ActiveRecord::Base.instantiate_observers
end
end

instantiate_observers メソッドの定義はこちら。

# activerecord-2.3.3/lib/active_record/observer.rb
# Instantiate the global Active Record observers.
def instantiate_observers
return if @observers.blank?
@observers.each do |observer|
if observer.respond_to?(:to_sym) # Symbol or String
observer.to_s.camelize.constantize.instance
elsif observer.respond_to?(:instance)
observer.instance
else
raise ArgumentError, "#{observer} must be a lowercase, underscored class name (or an instance of the class itself) responding to the instance method. Example: Person.observers = :big_brother # calls BigBrother.instance"
end
end
end

Observerクラス名(シンボル or String) から、Observerクラスのインスタンスを生成しています。

Observer のわかりやすい説明がこちら

[Rails][CodeReading] Railsの初期化コードを読む (イントロ&目次)

0 件のコメント:

コメントを投稿