`
CharlesCui
  • 浏览: 418155 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

RAILS类的装载程序

阅读更多

RAILS类的装载程序

  在纯RUBY中,一个脚本文件不需要用特殊的方式命名去匹配他的内容。然而,在RAILS中,你必须注意,总是在RUBY件名和文件所包含的类有个很直接的关联。RAILS充分利用RUBY所提供的无效常量回收机制。当RAILS在代码偶尔遇到一个未定义的常量,它使用一个基于文件命名协定的类加载器例程来寻找来加载所需的RUBY脚本。

  类加载器怎样知道去哪搜寻?我们已经在本章稍早讨论initializer.rb的启动过程规则的时就覆盖说明它 。RAILS有装载路径这概念,默认装载路径包括基本目录(你认为在你的程序会增加代码的任何地方)。默认的装载路径的方法命令会显示RAILS在他的默认路径怎样搜寻目录。我们将剖析这个方法的源代码并且解释每个加载行为的原因。

  test/mocks目录(在第17章”Testing”会广泛的涉及到)可以使你去忽略标准RAILS类行为。

paths = ["#{root_path}/test/mocks/#{environment}"]

# Add the app’s controller directory

paths.concat(Dir["#{root_path}/app/controllers/"])

# Then components subdirectories.

paths.concat(Dir["#{root_path}/components/[_a-z]*”])

# Followed by the standard includes.

paths.concat %w(

app

app/models

app/controllers

app/helpers

app/services

app/apis

components

config

lib

vendor

).map { |dir| “#{root_path}/#{dir}” }.select { |dir|

File.directory?(dir) }

paths.concat Dir["#{root_path}/vendor/plugins/*/lib/"]

paths.concat builtin_directories

end

想要看你的项目装载路径?只要在控制台如下输入$:  :

$ console

Loading development environment.

>> $:

  • ð [”/usr/local/lib/ruby/gems/1.8/gems/ … # about 20 lines of output

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics