Module: Metasploit::Model::Search::ClassMethods

Defined in:
lib/metasploit/model/search.rb

Overview

Allows operators registered with Association::ClassMethods#search_association and Attribute::ClassMethods#search_attribute to be looked up by name.

Instance Method Summary collapse

Instance Method Details

#search_operator_by_nameHash{Symbol => Metasploit::Model::Search::Operator}

Collects all search attributes from search associations and all attributes from this class to show the valid search operators to search.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/metasploit/model/search.rb', line 20

def search_operator_by_name
  unless instance_variable_defined? :@search_operator_by_name
    @search_operator_by_name = {}

    search_with_operator_by_name.each_value do |operator|
      @search_operator_by_name[operator.name] = operator
    end

    search_association_set.each do |association|
      begin
        reflection = reflect_on_association(association)
      rescue NameError
        raise NameError,
              "#{self} does not respond to reflect_on_association.  " \
              "It can be added to ActiveModels by including Metasploit::Model::Association into the class."
      end

      unless reflection
        raise Metasploit::Model::Association::Error.new(:model => self, :name => association)
      end

      association_class = reflection.klass

      # don't use search_operator_by_name as association operators on operators won't work
      association_class.search_with_operator_by_name.each_value do |with_operator|
        # non-attribute operators on association are assumed not to work
        if with_operator.respond_to? :attribute
          association_operator = Metasploit::Model::Search::Operator::Association.new(
              :association => association,
              :attribute_operator => with_operator,
              :klass => self
          )
          @search_operator_by_name[association_operator.name] = association_operator
        end
      end
    end
  end

  @search_operator_by_name
end