LoadingModules implement namespace-safe dynamic loading. They support automatic loading via const_missing, allowing contained items to be automatically loaded when required. No extra syntax is required, as expressions such as Controller::Admin::UserController load the relavent files automatically.
Ruby-style modules are supported, as a folder named ‘submodule’ will load ‘submodule.rb’ when available.
Methods
Attributes
| [R] | path | |
| [R] | root |
Public Class methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 59
59: def initialize(root, path=[])
60: @path = path.clone.freeze
61: @root = root
62: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 55
55: def self.root(*load_paths)
56: RootLoadingModule.new(*load_paths)
57: end
Public Instance methods
Is this name present or loadable? This method is used by Routes to find valid controllers.
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 105
105: def const_available?(name)
106: self.const_defined?(name) || load_paths.any? {|lp| lp.filesystem_path(path + [name])}
107: end
Load the controller class or a parent module.
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 73
73: def const_load!(name, file_name = nil)
74: path = self.path + [file_name || name]
75:
76: load_paths.each do |load_path|
77: fs_path = load_path.filesystem_path(path)
78: next unless fs_path
79:
80: if File.directory?(fs_path)
81: new_module = LoadingModule.new(self.root, self.path + [name])
82: self.const_set name, new_module
83: if self.root?
84: if Object.const_defined?(name)
85: msg = "Cannot load module #{name}: Object::#{name} is set to #{Object.const_get(name).inspect}"
86: raise NameError, msg
87: end
88: Object.const_set(name, new_module)
89: end
90: break
91: elsif File.file?(fs_path)
92: self.root.load_file!(fs_path)
93:
94: # Import the loaded constant from Object provided we are the root node.
95: self.const_set(name, Object.const_get(name)) if self.root? && Object.const_defined?(name)
96: break
97: end
98: end
99:
100: return self.const_defined?(name)
101: end
Load missing constants if possible.
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 68
68: def const_missing(name)
69: const_load!(name) ? const_get(name) : super(name)
70: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 65
65: def load_paths() self.root.load_paths end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/dependencies.rb, line 64
64: def root?() self.root == self end