Class: Metasploit::Model::Configuration::Autoload

Inherits:
Child
  • Object
show all
Defined in:
lib/metasploit/model/configuration/autoload.rb

Overview

Defines methods for adding paths to ActiveSupport::Dependencies.autoload_paths.

Instance Attribute Summary collapse

Attributes inherited from Child

#configuration

Instance Method Summary collapse

Instance Attribute Details

#relative_once_pathsArray<String>

Paths relative to Metasploit::Model::Configuration#root that are to be added to ActiveSupport::Dependencies.autoload_paths and ActiveSupport::Dependencies.autoload_once_paths.

Returns:

  • (Array<String>)

    Defaults to ['lib']



# File 'lib/metasploit/model/configuration/autoload.rb', line 9

#relative_pathsArray<String>

Paths relative to Metasploit::Model::Configuration#root that are to be added to ActiveSupport::Dependencies.autoload_paths, but not ActiveSupport::Dependencies.autoload_once_paths

Returns:

  • (Array<String>)

    Defaults to ['app/models']



# File 'lib/metasploit/model/configuration/autoload.rb', line 15

Instance Method Details

#all_pathsArray<String>

Combines #once_paths and #paths as both need to be added to ActiveSupport::Dependencies.autoload_paths.

Returns:

  • (Array<String>)


29
30
31
# File 'lib/metasploit/model/configuration/autoload.rb', line 29

def all_paths
  @all_paths ||= (once_paths + paths).uniq
end

#eager_load!void

This method returns an undefined value.

Eager loads all rb files under #all_paths. Paths in #all_paths are sorted before loading, so that app will load before lib. Files are required using ActiveSupport::Dependencies::Loadable#require_dependency so they interact with ActiveSupport::Dependencies loading correctly.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/metasploit/model/configuration/autoload.rb', line 38

def eager_load!
  # sort to favor app over lib since it is assumed that app/models will define classes and lib will define modules
  # included in those classes that are defined under the class namespaces, so the class needs to be required first
  all_paths.sort.each do |load_path|
    matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/

    Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
      require_dependency file.sub(matcher, '\1')
    end
  end
end

#once_pathsArray<String>

#relative_once_paths converted to absolute paths.

Returns:

  • (Array<String>)


53
54
55
56
57
# File 'lib/metasploit/model/configuration/autoload.rb', line 53

def once_paths
  @once_paths ||= relative_once_paths.collect { |relative_path|
    configuration.root.join(relative_path).to_path
  }
end

#pathsArray<String>

#relative_paths converted to absolute paths.

Returns:

  • (Array<String>)


62
63
64
65
66
# File 'lib/metasploit/model/configuration/autoload.rb', line 62

def paths
  @paths ||= relative_paths.collect { |relative_path|
    configuration.root.join(relative_path).to_path
  }
end

#setupvoid

This method returns an undefined value.

Adds #all_paths to ActiveSupport::Dependencies.autoload_paths if they are not already there and adds #once_paths to ActiveSupport::Dependencies.autoload_once_paths if they are not already there.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/metasploit/model/configuration/autoload.rb', line 96

def setup
  all_paths.each do |autoload_path|
    unless ActiveSupport::Dependencies.autoload_paths.include? autoload_path
      ActiveSupport::Dependencies.autoload_paths << autoload_path
    end
  end

  once_paths.each do |autoload_once_path|
    unless ActiveSupport::Dependencies.autoload_once_paths.include? autoload_once_path
      ActiveSupport::Dependencies.autoload_once_paths << autoload_once_path
    end
  end
end