Module: Metasploit::Model::Search::Operator

Extended by:
ActiveSupport::Autoload
Defined in:
lib/metasploit/model/search/operator.rb

Overview

Declaring operator classes

Interface

Operators do not need to subclass any specific superclass, but they are expected to define certain methods.

class MyOperator
  #
  # Instance Methods
  #

  # @param klass [Class] The klass on which `search_with` was called.
  def initialize(attributes={})
    # ...
  end

  # Description of what this operator searches for.
  #
  # @return [String]
  def help
    # ...
  end

  # Name of this operator.  The name of the operator is matched to the string before the ':' in a formatted
  # operation.
  #
  # @return [Symbol]
  def name
    # ...
  end

  # Creates a one or more operations based on `formatted_value`.
  #
  # @return [#operator, Array<#operator>] Operation with this operator as the operation's `operator`.
  def operate_on(formatted_value)
    # ...
  end
end

Help

Instead of having define your own #help method for your operator Class, you can include Help.

This allows the help to be looked up using I18n, and for the help to be customized based on the following criteria:

klass on which the operator is declared, including any Module#ancestors and the operator name

# config/locales/<lang>.yml
<lang>:
  <klass.i18n_scope>:
    ancestors:
      <klass_ancestor.model_name.i18n_key>:
        search:
          operator:
            names:
              <name>:
                help: "Help for searching <name> on <klass>"

class of the operator, including any Module#ancestors and the operator name

# config/locales/<lang>.yml
<lang>:
  <operator.class.i18n_scope>:
    search:
      operator:
        ancestors:
          <operator_class_ancestor.model_name.i18n_key>:
            <name>:
              help: "Help for searching <name> using <operator.class>"

class of the operator, including any Module#ancestors without the operator name

# config/locales/<lang>.yml
<lang>:
  <operator.class.i18n_scope>:
    search:
      operator:
        ancestors:
          <operator_class_ancestor.model_name.i18n_key>:
            help: "Help for searching using <operator.class>"

Base

Instead of writing an operator completely from scratch, you can subclass Base.

class MyOperator < Metasploit::Model::Search::Operator::Base
  # Name of this operator.  The name of the operator is matched to the string before the ':' in a formatted
  # operation.
  #
  # @return [Symbol]
  def name
    # ...
  end

  # Creates a one or more operations based on `formatted_value`.
  #
  # @return [#operator, Array<#operator>] Operation with this operator as the operation's `operator`.
  def operate_on(formatted_value)
    # ...
  end
end

Single

If all you want do is customize the name and operation Class that your custom operator class returns from #operate_on, then you can subclass Single instead of Base.

class MyOperator < Metasploit::Model::Search::Operator::Single
  # Name of this operator.  The name of the operator is matched to the string before the ':' in a formatted
  # operation.
  #
  # @return [Symbol]
  def name
    # ...
  end

  # `Class.name` of `Class` returned from {Metasploit::Model::Search::Operator::Single#operate_on}.
  #
  # @return [String] a `Class.name`
  def operation_class_name
    # ...
  end
end

Defined Under Namespace

Modules: Deprecated, Group, Help Classes: Association, Attribute, Base, Delegation, Null, Single