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


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

def initialize(root) @root = root end

Instance Method Details

#const_name_to_file_name(name) ⇒ Object


160
161
162
# File 'lib/active_support/dependencies.rb', line 160

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

#const_name_to_module_name(name) ⇒ Object


164
165
166
# File 'lib/active_support/dependencies.rb', line 164

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.


146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/active_support/dependencies.rb', line 146

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