Class: Metasploit::Concern::Loader
- Inherits:
-
Object
- Object
- Metasploit::Concern::Loader
- Includes:
- ActiveModel::Validations
- Defined in:
- app/models/metasploit/concern/loader.rb
Overview
Loads ActiveSupport::Concerns (or anything that can be passed to ‘include` really) under #root
Instance Attribute Summary collapse
-
#root ⇒ Pathname
Pathname under which to find concerns.
Instance Method Summary collapse
-
#each_pathname_constant(parent_pathname) {|constant| ... } ⇒ void
Yields each constant under ‘parent_pathname`.
-
#glob ⇒ Pathname
Glob pattern for concerns.
-
#initialize(attributes = {}) ⇒ Loader
constructor
A new instance of Loader.
-
#module_pathname_set ⇒ Set<Pathname>
Set of Pathnames for ‘Module`s that will have concerns included.
-
#register ⇒ void
Registers load hooks with ‘ActiveSupport.on_load`.
Constructor Details
#initialize(attributes = {}) ⇒ Loader
Returns a new instance of Loader.
51 52 53 54 55 |
# File 'app/models/metasploit/concern/loader.rb', line 51 def initialize(attributes={}) attributes.each do |attribute, value| public_send("#{attribute}=", value) end end |
Instance Attribute Details
#root ⇒ Pathname
Pathname under which to find concerns.
13 14 15 |
# File 'app/models/metasploit/concern/loader.rb', line 13 def root @root end |
Instance Method Details
#each_pathname_constant(parent_pathname) {|constant| ... } ⇒ void
This method returns an undefined value.
Yields each constant under ‘parent_pathname`.
33 34 35 36 37 38 39 40 41 |
# File 'app/models/metasploit/concern/loader.rb', line 33 def each_pathname_constant(parent_pathname) parent_pathname.each_child do |child_pathname| constant = constantize_pathname(child_pathname) if constant yield constant end end end |
#glob ⇒ Pathname
Glob pattern for concerns.
46 47 48 |
# File 'app/models/metasploit/concern/loader.rb', line 46 def glob root.join('**', '*.rb') end |
#module_pathname_set ⇒ Set<Pathname>
Set of Pathnames for ‘Module`s that will have concerns included.
60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/metasploit/concern/loader.rb', line 60 def module_pathname_set concern_paths = Dir.glob(glob) concern_paths.each_with_object(Set.new) { |concern_path, module_pathname_set| concern_pathname = Pathname.new(concern_path) module_pathname = concern_pathname.parent module_pathname_set.add module_pathname } end |
#register ⇒ void
This method returns an undefined value.
Registers load hooks with ‘ActiveSupport.on_load`.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/metasploit/concern/loader.rb', line 74 def register module_pathname_set.each do |module_pathname| relative_module_pathname = module_pathname.relative_path_from(root) relative_module_path = relative_module_pathname.to_path underscored_module_name = relative_module_path.gsub(File::SEPARATOR, '_') on_load_name = underscored_module_name.to_sym # on_load block is instance_evaled, so need to capture self loader = self ActiveSupport.on_load(on_load_name) do loader.each_pathname_constant(module_pathname) do |concern| include concern end if defined?(Rails) && Rails.env.development? unloadable end end end end |