Class: Metasploit::Model::Search::Operator::Single

Inherits:
Base
  • Object
show all
Defined in:
app/models/metasploit/model/search/operator/single.rb

Overview

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

Direct Known Subclasses

Attribute, Null

Constant Summary collapse

MODULE_SEPARATOR =

Separator between parent and child module/class names.

'::'
OPERATION_NAMESPACE_NAME =

Name of namespace module for operations returned from #operation_class and used by #operate_on.

"Metasploit::Model::Search::Operation"

Instance Attribute Summary

Attributes inherited from Base

#klass

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#name

Methods included from Help

#help

Methods inherited from Base

#initialize, #valid!

Constructor Details

This class inherits a constructor from Metasploit::Model::Base

Class Method Details

.constant_name(type) ⇒ String

The constant name for the given type.

Parameters:

  • type (Symbol, Hash)

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/metasploit/model/search/operator/single.rb', line 40

def self.constant_name(type)
  case type
    when Hash
      if type.length < 1
        raise ArgumentError, "Cannot destructure a Hash without entries"
      end

      if type.length > 1
        raise ArgumentError, "Cannot destructure a Hash with multiple entries"
      end

      partial_types = type.first
      partial_constant_names = partial_types.collect { |partial_type|
        constant_name(partial_type)
      }

      partial_constant_names.join(MODULE_SEPARATOR)
    when Symbol
      type.to_s.camelize
    else
      raise ArgumentError, "Can only convert Hashes and Symbols to constant names, not #{type.inspect}"
  end
end

Instance Method Details

#operate_on(formatted_value) ⇒ Metasploit::Model::Search::Operation::Base

Creates an operation of the correct type for this operator's #type.

Parameters:

Returns:



69
70
71
72
73
74
# File 'app/models/metasploit/model/search/operator/single.rb', line 69

def operate_on(formatted_value)
  operation_class.new(
      :value => formatted_value,
      :operator => self
  )
end

#operation_classClass<Metasploit::Model::Search::Operation::Base> (protected)

Returns:

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
# File 'app/models/metasploit/model/search/operator/single.rb', line 92

def operation_class
  unless instance_variable_defined? :@operation_class
    @operation_class = operation_class_name.constantize
  end

  @operation_class
end

#operation_class_nameString (protected)

The name of the #type-specific Metasploit::Model::Search::Operation::Base subclass.

Returns:

  • (String)

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/metasploit/model/search/operator/single.rb', line 104

def operation_class_name
  unless instance_variable_defined? :@operation_class_name
    unless type
      raise ArgumentError, "#{self.class}##{__method__} cannot be derived for #{name} operator because its type is nil"
    end

    partial_constant_names = [OPERATION_NAMESPACE_NAME]
    partial_constant_names << self.class.constant_name(type)

    @operation_class_name = partial_constant_names.join(MODULE_SEPARATOR)
  end

  @operation_class_name
end

#typeSymbol

This method is abstract.

subclass and derive operator type.

Type of the attribute.

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)


82
83
84
# File 'app/models/metasploit/model/search/operator/single.rb', line 82

def type
  raise NotImplementedError
end