Lance.zyb Blog

A Programmer.

Railtie

SampleApp::Application < Rails::Application < Rails::Engine < Rails::Railtie

这里需要注意的地方有下面二个

一、include Initializable

二、method_missing

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class << self
  
  def instance
    @instance ||= new
  end
  protected
    # If the class method does not have a method, then send the method call
    # to the Railtie instance.
    def method_missing(name, *args, &block)
      if instance.respond_to?(name)
        instance.public_send(name, *args, &block)
      else
        super
      end
    end
end

instance methods 委托给 class 使用