Module: Metasploit::Concern

Extended by:
ActiveSupport::Autoload
Defined in:
lib/metasploit/concern.rb,
lib/metasploit/concern/engine.rb,
lib/metasploit/concern/version.rb

Overview

Automates the inclusion of concerns into classes and models from other `Rail::Engine`s by use of an app/concerns directory in the `Rails::Engine` that declares the concerns.

The `Class` or `Module` must support the use of concerns by calling Concern.run.

To include concerns from a Rails::Application add 'app/concerns' to paths and then declare concerns under app/concerns.

To include concerns from a Rails::Engine add 'app/concerns' to the paths and then declare concerns under app/concerns.

Examples:

engine_that_supports_concerns/app/models/model_that_supports_concerns.rb

class EngineThatSupportsConcerns::ModelThatSupportsConcerns
  # declared as last statement in body so that concerns can redefine everything in class
  Metasploit::Concern.run(self)
end

config/application.rb

config.paths.add 'app/concerns', autoload: true

Concern declared in application

# app/concerns/engine_that_supports_concerns/model_that_supports_concerns/concern_from_application.rb
module EngineThatSupportsConcerns::ModelThatSupportsConcerns::ConcernFromApplication
  extend ActiveSupport::Concern

  included do
    # run with self equal to EngineThatSupportsConcerns::ModelThatSupportsConcerns, but at the end of the class
    # definition, instead of at the beginning as would be the case with
    #   class EngineThatSupportsConcerns::ModelThatSupportsConcerns
    #     include EngineThatSupportsConcerns::ModelThatSupportsConcerns::ModelThatSupportsConcerns
    #
    #     # body
    #   end
  end
end

Rails::Engine configuration for app/concerns

# engine_defining_concerns/lib/engine_defining_concerns/engine.rb
module EngineDefiningConcerns
  class Engine < Rails::Engine
    config.paths.add 'app/concerns', autoload: true
  end
end

Concern declared in Rails::Engine

# engine_defining_concerns/app/concerns
module EngineThatSupportsConcerns::ModelThatSupportsConcerns::ConcernFromEngine
  extend ActiveSupport::Concern

  included do
    # run with self equal to EngineThatSupportsConcerns::ModelThatSupportsConcerns, but at the end of the class
    # definition, instead of at the beginning as would be the case with
    #   class EngineThatSupportsConcerns::ModelThatSupportsConcerns
    #     include EngineThatSupportsConcerns::ModelThatSupportsConcerns::ModelThatSupportsConcerns
    #
    #     # body
    #   end
  end
end

Defined Under Namespace

Modules: Error Classes: Engine, Loader

Constant Summary collapse

VERSION =

VERSION is managed by GemRelease

'4.0.3'

Class Method Summary collapse

Class Method Details

.rootPathname

Note:

If `Rails` is loaded and Engine is defined, just use `Metasploit::Concern::Engine.root`.

The root of the `metasploit-concern` gem's file tree.

Returns:

  • (Pathname)

98
99
100
# File 'lib/metasploit/concern.rb', line 98

def self.root
  @root ||= Pathname.new(__FILE__).parent.parent.parent
end

.run(klass) ⇒ void

Note:

As `ActiveSupport.run_load_hooks` is used, it is safe to call run prior to Metasploit::Concern's

Note:

`klass` must have a `Module#name` so that the load hook symbol can be derived.

This method returns an undefined value.

initializer registering all the load hooks as any late load hooks will run as they are registered.

Runs the load hooks setup to include app/concerns concerns this `klass`.

Parameters:

  • klass (Class)

    A class that should support loading concerns from app/concerns.


112
113
114
115
# File 'lib/metasploit/concern.rb', line 112

def self.run(klass)
  load_hook_name = klass.name.underscore.gsub('/', '_').to_sym
  ActiveSupport.run_load_hooks(load_hook_name, klass)
end

.versionString

returns the VERSION

Returns:

  • (String)

11
12
13
# File 'lib/metasploit/concern/version.rb', line 11

def self.version
  VERSION
end