Class: Dependencies::ConstantLoadPath

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

Overview

This object defines a path from which Constants can be loaded.

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ ConstantLoadPath

Create a new load path with the filesystem path


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

def initialize(root) @root = root end

Instance Method Details

#const_name_to_file_name(name) ⇒ Object


167
168
169
# File 'lib/active_support/dependencies.rb', line 167

def const_name_to_file_name(name)
  name.to_s.underscore + '.rb'
end

#const_name_to_module_name(name) ⇒ Object


171
172
173
# File 'lib/active_support/dependencies.rb', line 171

def const_name_to_module_name(name)
  name.to_s.underscore
end

#filesystem_path(path, allow_module = true) ⇒ Object

Return nil if the path does not exist, or the path to a directory if the path leads to a module, or the path to a file if it leads to an object.


153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_support/dependencies.rb', line 153

def filesystem_path(path, allow_module=true)
  fs_path = [@root]
  fs_path += path[0..-2].map {|name| const_name_to_module_name name}

  if allow_module
    result = File.join(fs_path, const_name_to_module_name(path.last))
    return result if File.directory? result # Return the module path if one exists
  end

  result = File.join(fs_path, const_name_to_file_name(path.last))

  File.file?(result) ? result : nil
end