Class: Dependencies::LoadingModule

Inherits:
Module show all
Defined in:
lib/active_support/dependencies.rb

Overview

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.

Direct Known Subclasses

RootLoadingModule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#mattr_accessor, #mattr_reader, #mattr_writer

Constructor Details

#initialize(root, path = []) ⇒ LoadingModule

Returns a new instance of LoadingModule.



60
61
62
63
# File 'lib/active_support/dependencies.rb', line 60

def initialize(root, path=[])
  @path = path.clone.freeze
  @root = root
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



53
54
55
# File 'lib/active_support/dependencies.rb', line 53

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



54
55
56
# File 'lib/active_support/dependencies.rb', line 54

def root
  @root
end

Class Method Details

.root(*load_paths) ⇒ Object



56
57
58
# File 'lib/active_support/dependencies.rb', line 56

def self.root(*load_paths)
  RootLoadingModule.new(*load_paths)
end

Instance Method Details

#const_available?(name) ⇒ Boolean

Is this name present or loadable? This method is used by Routes to find valid controllers.

Returns:

  • (Boolean)


94
95
96
# File 'lib/active_support/dependencies.rb', line 94

def const_available?(name)
  self.const_defined?(name) || load_paths.any? {|lp| lp.filesystem_path(path + [name])}
end

#const_load!(name, file_name = nil) ⇒ Object

Load the controller class or a parent module.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_support/dependencies.rb', line 73

def const_load!(name, file_name = nil)
  path = self.path + [file_name || name]

  load_paths.each do |load_path|
    fs_path = load_path.filesystem_path(path)
    next unless fs_path

    if File.directory?(fs_path)
      self.const_set name, LoadingModule.new(self.root, self.path + [name])
      break
    elsif File.file?(fs_path)
      self.root.load_file!(fs_path)
      break
    end
  end

  return self.const_defined?(name)
end

#const_missing(name) ⇒ Object

Load missing constants if possible.



68
69
70
# File 'lib/active_support/dependencies.rb', line 68

def const_missing(name)
  const_load!(name) ? const_get(name) : super(name)
end

#load_pathsObject



65
# File 'lib/active_support/dependencies.rb', line 65

def load_paths() self.root.load_paths end