跳至内容 跳至搜索

此对象是一个扩展的哈希,它作为 Rails::Paths 系统的根目录。它允许您通过类似哈希的 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_loadautoloadautoload_onceglob

最后,该 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

属性

[RW] path

类公共方法

new(path)

# File railties/lib/rails/paths.rb, line 54
def initialize(path)
  @path = path
  @root = {}
end

实例公共方法

[](path)

# File railties/lib/rails/paths.rb, line 69
def [](path)
  @root[path]
end

[]=(path, value)

# 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 = {})

# 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()

# File railties/lib/rails/paths.rb, line 85
def all_paths
  values.tap(&:uniq!)
end

autoload_once()

# File railties/lib/rails/paths.rb, line 89
def autoload_once
  filter_by(&:autoload_once?)
end

autoload_paths()

# File railties/lib/rails/paths.rb, line 97
def autoload_paths
  filter_by(&:autoload?)
end

eager_load()

# File railties/lib/rails/paths.rb, line 93
def eager_load
  filter_by(&:eager_load?)
end

keys()

# File railties/lib/rails/paths.rb, line 77
def keys
  @root.keys
end

load_paths()

# File railties/lib/rails/paths.rb, line 101
def load_paths
  filter_by(&:load_path?)
end

values()

# File railties/lib/rails/paths.rb, line 73
def values
  @root.values
end

values_at(*list)

# File railties/lib/rails/paths.rb, line 81
def values_at(*list)
  @root.values_at(*list)
end