Rails::Paths::Root (original) (raw)
This object is an extended hash that behaves as root of the Rails::Paths system. It allows you to collect information about how you want to structure your application paths by a Hash like API. It requires you to give a physical path on initialization.
root = Root.new root.add "app/controllers", :eager_load => true
The command above creates a new root object and add “app/controllers” as a path. This means we can get a Path object back like below:
path = root["app/controllers"]
path.eager_load?
path.is_a?(Rails::Paths::Path)
The Path object is simply an array and allows you to easily add extra paths:
path.is_a?(Array) path.inspect
path << "lib/controllers" path.inspect
Notice that when you add a path using #add, the path object created already contains the path with the same path value given to #add. In some situations, you may not want this behavior, so you can give :with as option.
root.add "config/routes", :with => "config/routes.rb" root["config/routes"].inspect
The #add method accepts the following options as arguments: #eager_load, autoload, #autoload_once and glob.
Finally, the Path object also provides a few helpers:
root = Root.new root.path = "/rails" root.add "app/controllers"
root["app/controllers"].expanded root["app/controllers"].existent
Check the Path documentation for more information.
Methods
A
E
F
L
N
Attributes
Class Public methods
new(path)
Source: show
def initialize(path) raise if path.is_a?(Array) @current = nil @path = path @root = self super() end
Instance Public methods
[]=(path, value)
Source: show
def []=(path, value) value = Path.new(self, path, value) unless value.is_a?(Path) super(path, value) end
add(path, options={})
Source: show
def add(path, options={}) with = options[:with] || path self[path] = Path.new(self, path, with, options) end
all_paths()
Source: show
def all_paths values.tap { |v| v.uniq! } end
autoload_once()
Source: show
def autoload_once filter_by(:autoload_once?) end
autoload_paths()
Source: show
def autoload_paths filter_by(:autoload?) end
eager_load()
Source: show
def eager_load filter_by(:eager_load?) end
load_paths()
Source: show
def load_paths filter_by(:load_path?) end
Instance Protected methods
filter_by(constraint)
Source: show
def filter_by(constraint) all = [] all_paths.each do |path| if path.send(constraint) paths = path.existent paths -= path.children.map { |p| p.send(constraint) ? [] : p.existent }.flatten all.concat(paths) end end all.uniq! all end