Class: Metasploit::Model::Search::Query
- Defined in:
- app/models/metasploit/model/search/query.rb
Overview
A search query on a #klass. Parses query from #formatted string.
Instance Attribute Summary collapse
-
#formatted ⇒ String
Query string containing space separated
: pairs. -
#formatted_operations ⇒ Array<String>
#formatted broken up into individual operation (
: ) Strings. -
#klass ⇒ Class, #search_operator_by_name
The klass that is being searched.
-
#operations ⇒ Array<Metasploit::Model::Search::Operation::Base>
Parses #formatted to create search operations that can validate if the value is correct the operation's operator's type.
Class Method Summary collapse
-
.formatted_operations(formatted) ⇒ Array<String>
Parses #formatted into a list of formatted operation composed of
: Strings.
Instance Method Summary collapse
-
#operations_by_operator ⇒ Hash{Metasploit::Model::Search::Operator::Base => Metasploit::Model::Search::Operation::Base}
using unknown operators} with the same name should not be grouped together when processing this query into an actual search of record and/or models.
-
#operations_valid ⇒ void
private
Validates that all #operations are valid.
-
#parse_operator(formatted_operator) ⇒ Metasploit::Model::Search::Operation::Base, Metasploit::Model::Search::Operator::Null
Converts formatted operator extracted from formatted operation to its Operator::Base instance.
-
#tree ⇒ Metasploit::Model::Search::Group::Intersection<Metasploit::Model::Search::Group::Union<Metasploit::Model::Search::Operation::Base>>
Groups #operations together by Operation::Base#operator into unions that are intersected.
-
#without_operator(operator) ⇒ Metasploit::Model:Search::Query
Returns a new query with all #operations on the given
operatorremoved.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Metasploit::Model::Base
Instance Attribute Details
#formatted ⇒ String
Query string containing space separated
11 12 13 |
# File 'app/models/metasploit/model/search/query.rb', line 11 def formatted @formatted end |
#formatted_operations ⇒ Array<String>
#formatted broken up into individual operation (
19 |
# File 'app/models/metasploit/model/search/query.rb', line 19 attr_writer :formatted_operations |
#klass ⇒ Class, #search_operator_by_name
The klass that is being searched.
25 26 27 |
# File 'app/models/metasploit/model/search/query.rb', line 25 def klass @klass end |
#operations ⇒ Array<Metasploit::Model::Search::Operation::Base>
Parses #formatted to create search operations that can validate if the value is correct the operation's operator's type.
31 |
# File 'app/models/metasploit/model/search/query.rb', line 31 attr_writer :operations |
Class Method Details
.formatted_operations(formatted) ⇒ Array<String>
Parses #formatted into a list of formatted operation composed of
57 58 59 |
# File 'app/models/metasploit/model/search/query.rb', line 57 def self.formatted_operations(formatted) Shellwords.shellsplit(formatted.to_s) end |
Instance Method Details
#operations_by_operator ⇒ Hash{Metasploit::Model::Search::Operator::Base => Metasploit::Model::Search::Operation::Base}
Query is validated before grouping the operations as {Metasploit::Model::Search::Operation::Null operation
using unknown operators} with the same name should not be grouped together when processing this query into an actual search of record and/or models.
Groups #operations together by Operation::Base#operator.
95 96 97 98 99 100 101 102 103 |
# File 'app/models/metasploit/model/search/query.rb', line 95 def operations_by_operator unless instance_variable_defined? :@operations_by_operator valid! @operations_by_operator = operations.group_by(&:operator) end @operations_by_operator ||= operations.group_by(&:operator) end |
#operations_valid ⇒ void (private)
This method returns an undefined value.
Validates that all #operations are valid.
166 167 168 169 170 |
# File 'app/models/metasploit/model/search/query.rb', line 166 def operations_valid unless operations.all?(&:valid?) errors.add(:operations, :invalid) end end |
#parse_operator(formatted_operator) ⇒ Metasploit::Model::Search::Operation::Base, Metasploit::Model::Search::Operator::Null
Converts formatted operator extracted from formatted operation to its Operator::Base instance.
113 114 115 116 117 118 119 120 121 122 |
# File 'app/models/metasploit/model/search/query.rb', line 113 def parse_operator(formatted_operator) operator_name = formatted_operator.to_sym operator = klass.search_operator_by_name[operator_name] unless operator operator = Metasploit::Model::Search::Operator::Null.new(:name => operator_name) end operator end |
#tree ⇒ Metasploit::Model::Search::Group::Intersection<Metasploit::Model::Search::Group::Union<Metasploit::Model::Search::Operation::Base>>
Groups #operations together by Operation::Base#operator into unions that are intersected.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/metasploit/model/search/query.rb', line 129 def tree unless instance_variable_defined? :@tree unions = operations_by_operator.collect do |_operator, operations| Metasploit::Model::Search::Group::Union.new(:children => operations) end @tree = Metasploit::Model::Search::Group::Intersection.new(:children => unions) end @tree end |
#without_operator(operator) ⇒ Metasploit::Model:Search::Query
Returns a new query with all #operations on the given operator removed.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/models/metasploit/model/search/query.rb', line 146 def without_operator(operator) operations = operations_by_operator[operator] if operations filtered_operations = self.operations - operations self.class.new( klass: klass, operations: filtered_operations ) else self end end |