Module: Metasploit::Model::Search
- Extended by:
- ActiveSupport::Autoload, ActiveSupport::Concern
- Includes:
- Association, Attribute, With
- Defined in:
- lib/metasploit/model/search.rb,
lib/metasploit/model/search/with.rb,
lib/metasploit/model/search/operator.rb,
lib/metasploit/model/search/attribute.rb,
lib/metasploit/model/search/operation.rb,
lib/metasploit/model/search/association.rb,
lib/metasploit/model/search/operator/help.rb
Overview
DSL to define associations and attributes that can be searched. Making an association searchable, will expose the attributes that association's class defined as searchable.
Operators
Search operators define how to search against a given Class
.
Attributes
Boolean, Date
, Integer
, and String
attributes can be searched with
search_attribute. Integer
and String
attributes can be further restricted to a defined Set
of values.
class Part
include Metasploit::Model::Search
search_attribute :part,
:integer
end
The above defines the :part
operator on Part
.
Custom search operators
If a search operator does not directly correspond to an attribute or a the attribute needs custom validation, then a custom operator class can be setup as the search operator
class Search::Operator::UUID
def name
:uuid
end
end
class Part
include Metasploit::Model::Search
search_with Search::Operator::UUID
end
The above defines the :uuid
operator on Part
.
Associations
Search operators registered with
search_attribute or
search_with on an associated Class
can be searched
with Association::ClassMethods#search_association:
class Widget
include Metasploit::Model::Search
# declare parts association
search_association :parts
end
The above will define the :'parts.number'
and :'parts.uuid'
operator on Widget
.
Queries
Once search operators are defined, a formatted
query, composed of space separated formatted operation, <operator.name>:<formatted_value>
, can be parsed to
produce a validatable query.
query = Metasploit::Model::Search::Query.new(
formatted: 'parts.number:1 parts.number:2 parts.uuid:EX,QR'
)
Operations using the same operator are unioned together, while operations with different operator are intersected
together, so the above formatted query can be thought of as
(parts.number:1 || parts.number:2) && parts.uuid:EX,QR
.
Results
Once a Query is defined, it needs to be converted to a data store specific visitor.
Visitors for ActiveRecord
are defined in
MetasploitDataModels::Search::Visitor.
If you want to define your own visitors, you can subclass Visitation::Visitor.
Defined Under Namespace
Modules: Association, Attribute, ClassMethods, Group, Operation, Operator, With Classes: Query