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.# config/locales/<lang>.yml <lang>: <Class#i18n_scope>: ancestors: <ancestor.model_name.i18n_key>: search: operator: names: <attribute>: help: "The attribute on the class"
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
:# spec/spec_helper.rb support_glob = Metasploit::Model::Engine.root.join('spec', 'support', '**', '*.rb') Dir.glob(support_glob) do |path| require path end
In the spec for the
Class
that calledsearch_attribute
, use the 'search_attribute' shared example by passing that arguments passed to #search_attribute.# spec/app/models/my_class_spec.rb require 'spec_helper' describe MyClass do context 'search' do context 'attributes' do it_should_behave_like 'search_attribute', type: { set: :string } end end end
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.
# config/locales/<lang>.yml
<lang>:
<Class#i18n_scope>:
ancestors:
<ancestor.model_name.i18n_key>:
search:
operator:
names:
<attribute>:
help: "The attribute on the class"
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
:
# spec/spec_helper.rb
support_glob = Metasploit::Model::Engine.root.join('spec', 'support', '**', '*.rb')
Dir.glob(support_glob) do |path|
require path
end
In the spec for the Class
that called search_attribute
, use the 'search_attribute' shared example by
passing that arguments passed to #search_attribute.
# spec/app/models/my_class_spec.rb
require 'spec_helper'
describe MyClass do
context 'search' do
context 'attributes' do
it_should_behave_like 'search_attribute',
type: {
set: :string
}
end
end
end
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 |