Class: Rails::Paths::Root
- Inherits:
-
Object
- Object
- Rails::Paths::Root
- Defined in:
- lib/rails/paths.rb
Overview
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 "/rails"
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 Rails::Paths::Path object back like below:
path = root["app/controllers"]
path.eager_load? # => true
path.is_a?(Rails::Paths::Path) # => true
The Path object is simply an enumerable and allows you to easily add extra paths:
path.is_a?(Enumerable) # => true
path.to_ary.inspect # => ["app/controllers"]
path << "lib/controllers"
path.to_ary.inspect # => ["app/controllers", "lib/controllers"]
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 # => ["config/routes.rb"]
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 "/rails"
root.add "app/controllers"
root["app/controllers"]. # => ["/rails/app/controllers"]
root["app/controllers"].existent # => ["/rails/app/controllers"]
Check the Rails::Paths::Path documentation for more information.
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #[](path) ⇒ Object
- #[]=(path, value) ⇒ Object
- #add(path, options = {}) ⇒ Object
- #all_paths ⇒ Object
- #autoload_once ⇒ Object
- #autoload_paths ⇒ Object
- #eager_load ⇒ Object
-
#initialize(path) ⇒ Root
constructor
A new instance of Root.
- #keys ⇒ Object
- #load_paths ⇒ Object
- #values ⇒ Object
- #values_at(*list) ⇒ Object
Constructor Details
#initialize(path) ⇒ Root
Returns a new instance of Root.
47 48 49 50 51 |
# File 'lib/rails/paths.rb', line 47 def initialize(path) @current = nil @path = path @root = {} end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
45 46 47 |
# File 'lib/rails/paths.rb', line 45 def path @path end |
Instance Method Details
#[](path) ⇒ Object
63 64 65 |
# File 'lib/rails/paths.rb', line 63 def [](path) @root[path] end |
#[]=(path, value) ⇒ Object
53 54 55 56 |
# File 'lib/rails/paths.rb', line 53 def []=(path, value) glob = self[path] ? self[path].glob : nil add(path, with: value, glob: glob) end |
#add(path, options = {}) ⇒ Object
58 59 60 61 |
# File 'lib/rails/paths.rb', line 58 def add(path, = {}) with = Array(.fetch(:with, path)) @root[path] = Path.new(self, path, with, ) end |
#all_paths ⇒ Object
79 80 81 |
# File 'lib/rails/paths.rb', line 79 def all_paths values.tap { |v| v.uniq! } end |
#autoload_once ⇒ Object
83 84 85 |
# File 'lib/rails/paths.rb', line 83 def autoload_once filter_by { |p| p.autoload_once? } end |
#autoload_paths ⇒ Object
91 92 93 |
# File 'lib/rails/paths.rb', line 91 def autoload_paths filter_by { |p| p.autoload? } end |
#eager_load ⇒ Object
87 88 89 |
# File 'lib/rails/paths.rb', line 87 def eager_load filter_by { |p| p.eager_load? } end |
#keys ⇒ Object
71 72 73 |
# File 'lib/rails/paths.rb', line 71 def keys @root.keys end |
#load_paths ⇒ Object
95 96 97 |
# File 'lib/rails/paths.rb', line 95 def load_paths filter_by { |p| p.load_path? } end |
#values ⇒ Object
67 68 69 |
# File 'lib/rails/paths.rb', line 67 def values @root.values end |
#values_at(*list) ⇒ Object
75 76 77 |
# File 'lib/rails/paths.rb', line 75 def values_at(*list) @root.values_at(*list) end |