这个对象是一个扩展的哈希,其行为类似于 Rails::Paths 系统的根。它允许您通过类似 Hash 的 API 来收集有关您想如何构建应用程序路径的信息。它要求您在初始化时提供一个物理路径。
root = Root.new "/rails" root.add "app/controllers", eager_load: true
上面的命令创建了一个新的根对象,并将“app/controllers”添加为一个路径。这意味着我们可以像下面这样获得一个 Rails::Paths::Path 对象:
path = root["app/controllers"] path.eager_load? # => true path.is_a?(Rails::Paths::Path) # => true
该 Path 对象只是一个可枚举对象,并允许您轻松添加额外的路径。
path.is_a?(Enumerable) # => true path.to_ary.inspect # => ["app/controllers"] path << "lib/controllers" path.to_ary.inspect # => ["app/controllers", "lib/controllers"]
请注意,当您使用 add 添加路径时,创建的 Path 对象已经包含了与传递给 add 的路径值相同的路径。在某些情况下,您可能不希望出现这种行为,因此您可以提供 :with 作为选项。
root.add "config/routes", with: "config/routes.rb" root["config/routes"].inspect # => ["config/routes.rb"]
add 方法接受以下选项作为参数: eager_load、autoload、autoload_once 和 glob。
最后,Path 对象还提供了一些辅助方法。
root = Root.new "/rails" root.add "app/controllers" root["app/controllers"].expanded # => ["/rails/app/controllers"] root["app/controllers"].existent # => ["/rails/app/controllers"]
有关更多信息,请查看 Rails::Paths::Path 文档。
方法
- #
- A
- E
- K
- L
- N
- V
Attributes
| [RW] | path |
类公共方法
new(path) 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 54 def initialize(path) @path = path @root = {} end
实例公共方法
[](path) 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 69 def [](path) @root[path] end
[]=(path, value) 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 59 def []=(path, value) glob = self[path] ? self[path].glob : nil add(path, with: value, glob: glob) end
add(path, options = {}) 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 64 def add(path, options = {}) with = Array(options.fetch(:with, path)) @root[path] = Path.new(self, path, with, options) end
all_paths() 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 85 def all_paths values.tap(&:uniq!) end
autoload_once() 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 89 def autoload_once filter_by(&:autoload_once?) end
autoload_paths() 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 97 def autoload_paths filter_by(&:autoload?) end
eager_load() 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 93 def eager_load filter_by(&:eager_load?) end
load_paths() 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 101 def load_paths filter_by(&:load_path?) end
values() 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 73 def values @root.values end
values_at(*list) 链接
源代码: 显示 | 在 GitHub 上
# File railties/lib/rails/paths.rb, line 81 def values_at(*list) @root.values_at(*list) end