Module: Metasploit::Model::Search::Attribute
- Extended by:
- ActiveSupport::Concern
- Includes:
- With
- Included in:
- Metasploit::Model::Search
- Defined in:
- lib/metasploit/model/search/attribute.rb
Overview
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
ClassMethods#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 ClassMethods#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
Defined Under Namespace
Modules: ClassMethods