此物件是一個擴展的雜湊,作為 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_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
屬性
[讀寫] | 路徑(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