我是 Rails 新手,在理解我正在从事的项目中的一些代码时非常迷失:
中routes.rb
,我有
ActionController::Routing::Routes.draw do |map|
...
map.filter "repository_owner_namespacing", :file => "route_filters/repository_owner_namespacing"
...
end
我试图理解一个方法around_recognize
是如何被调用的repository_owner_namespacing.rb
。后一个文件是这样开始的
require 'routing_filter/base'
module RoutingFilter
class RepositoryOwnerNamespacing < Base
...
def around_recognize
...
around_recognize
似乎是在 routing_filter.rb 中调用的,它的开头是这样的:
module RoutingFilter
mattr_accessor :active
@@active = true
class Chain < Array
def << (filter)
filter.successor = last
super
end
def run(method, *args, &final)
RoutingFilter.active ? last.run(method, *args, &final) : final.call
end
end
end
# allows to install a filter to the route set by calling: map.filter 'locale'
ActionController::Routing::RouteSet::Mapper.class_eval do
def filter(name, options = {})
require options.delete(:file) || "routing_filter/#{name}"
klass = RoutingFilter.const_get name.to_s.camelize
@set.filters << klass.new(options)
end
end
def filters
@filters ||= RoutingFilter::Chain.new
end
...
和 routoing_fiter/base.rb,有
module RoutingFilter
class Base
attr_accessor :successor, :options
def initialize(options)
@options = options
options.each{|name, value| instance_variable_set :"@#{name}", value }
end
def run(method, *args, &block)
successor = @successor ? lambda { @successor.run(method, *args, &block) } : block
send method, *args, &successor
end
end
end
我的问题是我真的不知道“last”设置在哪里(在filter.successor = last
),以及@set 设置在哪里。我在代码项目中找不到任何全面的跟踪。它是否对应于 ruby 或 rails 内置变量?(顺便说一句,这 @set.filters << klass.new(options)
对应于什么?)