Module: Metasploit::Model::Search::Attribute::ClassMethods
- Defined in:
- lib/metasploit/model/search/attribute.rb
Overview
Adds #search_attribute DSL to make attribute search operators.
Instance Method Summary collapse
-
#search_attribute(attribute, options = {}) ⇒ Metasploit::Model::Search::Operator::Base
Registers attributes that can be searched. Attributes must be declared to be searchable as a type from Operator::Attribute::TYPES. The type of the attribute is used to select a type-specific Operation, which will validate the Operation::Base#value is of the valid type.
Set attributes
Search attributes declared as having an integer set or string set type integer or string set require a
<attribute>_set
method to be defined on theClass
, which returns the set of allowed values for the search attribute's operation. This method will be called, indirectly by Operation::Set::Integer's and Operation::Set::String's validations.Help
The help for each operator is uses the
I18n
system, so the help for an attribute operator on a given class can added toconfig/locales/<lang>.yml
. The scope of the lookup, under the language key is theClass
'si18n_scope
, which ismetasploit.model
if theClass
includes Translation oractive_record
forApplicationRecord
subclasses. Under thei18n_scope
, anyModule#ancestor
'smodel_name.i18n_key
can be used to look up the help for an attribute's operator. This allows for super classes or mixins to define the search operator help for subclasses.<span class='comment val'># config/locales/<lang>.yml</span> <span class='lt op'><</span><span class='rubyid_lang identifier id'>lang</span><span class='gt op'>></span><span class='colon op'>:</span> <span class='lt op'><</span><span class='rubyid_Class constant id'>Class</span><span class='comment val'>#i18n_scope>:</span> <span class='label val'>ancestors:</span> <span class='lt op'><</span><span class='rubyid_ancestor identifier id'>ancestor</span><span class='dot token'>.</span><span class='rubyid_model_name identifier id'>model_name</span><span class='dot token'>.</span><span class='rubyid_i18n_key identifier id'>i18n_key</span><span class='gt op'>></span><span class='colon op'>:</span> <span class='label val'>search:</span> <span class='label val'>operator:</span> <span class='label val'>names:</span> <span class='lt op'><</span><span class='rubyid_attribute identifier id'>attribute</span><span class='gt op'>></span><span class='colon op'>:</span> <span class='label val'>help:</span> <span class='string val'>"The attribute on the class"</span>
Testing
#search_attribute calls can be tested with the 'search_attribute' shared example. First, ensure the shared examples from
metasploit-model
are required in yourspec_helper.rb
:<span class='comment val'># spec/spec_helper.rb</span> <span class='rubyid_support_glob identifier id'>support_glob</span> <span class='assign token'>=</span> <span class='rubyid_Metasploit constant id'>Metasploit</span><span class='colon2 op'>::</span><span class='rubyid_Model constant id'>Model</span><span class='colon2 op'>::</span><span class='rubyid_Engine constant id'>Engine</span><span class='dot token'>.</span><span class='rubyid_root identifier id'>root</span><span class='dot token'>.</span><span class='rubyid_join identifier id'>join</span><span class='lparen token'>(</span><span class='string val'>'spec'</span><span class='comma token'>,</span> <span class='string val'>'support'</span><span class='comma token'>,</span> <span class='string val'>'**'</span><span class='comma token'>,</span> <span class='string val'>'*.rb'</span><span class='rparen token'>)</span> <span class='rubyid_Dir constant id'>Dir</span><span class='dot token'>.</span><span class='rubyid_glob identifier id'>glob</span><span class='lparen token'>(</span><span class='rubyid_support_glob identifier id'>support_glob</span><span class='rparen token'>)</span> <span class='rubyid_do do kw'>do</span> <span class='bitor op'>|</span><span class='rubyid_path identifier id'>path</span><span class='bitor op'>|</span> <span class='rubyid_require identifier id'>require</span> <span class='rubyid_path identifier id'>path</span> <span class='rubyid_end end kw'>end</span>
In the spec for the
Class
that calledsearch_attribute
, use the 'search_attribute' shared example by passing that arguments passed to #search_attribute.<span class='comment val'># spec/app/models/my_class_spec.rb</span> <span class='rubyid_require identifier id'>require</span> <span class='string val'>'spec_helper'</span> <span class='rubyid_describe identifier id'>describe</span> <span class='rubyid_MyClass constant id'>MyClass</span> <span class='rubyid_do do kw'>do</span> <span class='rubyid_context identifier id'>context</span> <span class='string val'>'search'</span> <span class='rubyid_do do kw'>do</span> <span class='rubyid_context identifier id'>context</span> <span class='string val'>'attributes'</span> <span class='rubyid_do do kw'>do</span> <span class='rubyid_it_should_behave_like identifier id'>it_should_behave_like</span> <span class='string val'>'search_attribute'</span><span class='comma token'>,</span> <span class='label val'>type:</span> <span class='lbrace token'>{</span> <span class='label val'>set:</span> <span class='symbol val'>:string</span> <span class='rbrace token'>}</span> <span class='rubyid_end end kw'>end</span> <span class='rubyid_end end kw'>end</span> <span class='rubyid_end end kw'>end</span>
Instance Method Details
#search_attribute(attribute, options = {}) ⇒ Metasploit::Model::Search::Operator::Base
Registers attributes that can be searched. Attributes must be declared to be searchable as a type from Operator::Attribute::TYPES. The type of the attribute is used to select a type-specific Operation, which will validate the Operation::Base#value is of the valid type.
Set attributes
Search attributes declared as having an integer set or string set type integer or string set require a
<attribute>_set
method to be defined on the Class
, which returns the set of allowed values for the search
attribute's operation. This method will be called, indirectly by
Operation::Set::Integer's and Operation::Set::String's
validations.
Help
The help for each operator is uses the I18n
system, so the help for an attribute operator on a given class can
added to config/locales/<lang>.yml
. The scope of the lookup, under the language key is the Class
's
i18n_scope
, which is metasploit.model
if the Class
includes Translation or
active_record
for ApplicationRecord
subclasses. Under the i18n_scope
, any Module#ancestor
's
model_name.i18n_key
can be used to look up the help for an attribute's operator. This allows for super
classes or mixins to define the search operator help for subclasses.
<span class='comment val'># config/locales/<lang>.yml</span>
<span class='lt op'><</span><span class='rubyid_lang identifier id'>lang</span><span class='gt op'>></span><span class='colon op'>:</span>
<span class='lt op'><</span><span class='rubyid_Class constant id'>Class</span><span class='comment val'>#i18n_scope>:</span>
<span class='label val'>ancestors:</span>
<span class='lt op'><</span><span class='rubyid_ancestor identifier id'>ancestor</span><span class='dot token'>.</span><span class='rubyid_model_name identifier id'>model_name</span><span class='dot token'>.</span><span class='rubyid_i18n_key identifier id'>i18n_key</span><span class='gt op'>></span><span class='colon op'>:</span>
<span class='label val'>search:</span>
<span class='label val'>operator:</span>
<span class='label val'>names:</span>
<span class='lt op'><</span><span class='rubyid_attribute identifier id'>attribute</span><span class='gt op'>></span><span class='colon op'>:</span>
<span class='label val'>help:</span> <span class='string val'>"The attribute on the class"</span>
Testing
#search_attribute calls can be tested with the 'search_attribute' shared example. First, ensure
the shared examples from metasploit-model
are required in your spec_helper.rb
:
<span class='comment val'># spec/spec_helper.rb</span>
<span class='rubyid_support_glob identifier id'>support_glob</span> <span class='assign token'>=</span> <span class='rubyid_Metasploit constant id'>Metasploit</span><span class='colon2 op'>::</span><span class='rubyid_Model constant id'>Model</span><span class='colon2 op'>::</span><span class='rubyid_Engine constant id'>Engine</span><span class='dot token'>.</span><span class='rubyid_root identifier id'>root</span><span class='dot token'>.</span><span class='rubyid_join identifier id'>join</span><span class='lparen token'>(</span><span class='string val'>'spec'</span><span class='comma token'>,</span> <span class='string val'>'support'</span><span class='comma token'>,</span> <span class='string val'>'**'</span><span class='comma token'>,</span> <span class='string val'>'*.rb'</span><span class='rparen token'>)</span>
<span class='rubyid_Dir constant id'>Dir</span><span class='dot token'>.</span><span class='rubyid_glob identifier id'>glob</span><span class='lparen token'>(</span><span class='rubyid_support_glob identifier id'>support_glob</span><span class='rparen token'>)</span> <span class='rubyid_do do kw'>do</span> <span class='bitor op'>|</span><span class='rubyid_path identifier id'>path</span><span class='bitor op'>|</span>
<span class='rubyid_require identifier id'>require</span> <span class='rubyid_path identifier id'>path</span>
<span class='rubyid_end end kw'>end</span>
In the spec for the Class
that called search_attribute
, use the 'search_attribute' shared example by
passing that arguments passed to #search_attribute.
<span class='comment val'># spec/app/models/my_class_spec.rb</span>
<span class='rubyid_require identifier id'>require</span> <span class='string val'>'spec_helper'</span>
<span class='rubyid_describe identifier id'>describe</span> <span class='rubyid_MyClass constant id'>MyClass</span> <span class='rubyid_do do kw'>do</span>
<span class='rubyid_context identifier id'>context</span> <span class='string val'>'search'</span> <span class='rubyid_do do kw'>do</span>
<span class='rubyid_context identifier id'>context</span> <span class='string val'>'attributes'</span> <span class='rubyid_do do kw'>do</span>
<span class='rubyid_it_should_behave_like identifier id'>it_should_behave_like</span> <span class='string val'>'search_attribute'</span><span class='comma token'>,</span>
<span class='label val'>type:</span> <span class='lbrace token'>{</span>
<span class='label val'>set:</span> <span class='symbol val'>:string</span>
<span class='rbrace token'>}</span>
<span class='rubyid_end end kw'>end</span>
<span class='rubyid_end end kw'>end</span>
<span class='rubyid_end end kw'>end</span>
129 130 131 132 133 |
# File 'lib/metasploit/model/search/attribute.rb', line 129 def search_attribute(attribute, ={}) search_with Metasploit::Model::Search::Operator::Attribute, :attribute => attribute, :type => [:type] end |