Module: Metasploit::Model::Derivation

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Defined in:
lib/metasploit/model/derivation.rb,
lib/metasploit/model/derivation/full_name.rb

Overview

Allow to declare that attributes should be derived, which will set the attribute equal to derived_ if the attribute is nil before validation. Optionally, it can be checked if the attribute matches the derived_ in a validation.

Examples:

Full setup

class Person < ActiveRecord::Base
  include Metasploit::Model::Derivation

  #
  # Attributes
  #

  # @!attributes [rw] first_name
  #   First Name.
  #
  #   @return [String]

  # @!attribute [rw] full_name
  #   Full name. (Includes first and last name).
  #
  #   @return [String]

  # @!attribute [rw] last_name
  #   Last name.
  #
  #   @return [String]

  #
  # Derivations
  #

  derives :full_name

  #
  # Methods
  #

  # Derives {#full_name} from {#first_name} and {#last_name}.
  #
  # @return [String] "<first_name> <last_name>"
  def derive_full_name
    "#{first_name} #{last_name}"
  end
end

Defined Under Namespace

Modules: ClassMethods, FullName

Instance Method Summary collapse

Instance Method Details

#derivevoid (private)

This method returns an undefined value.

Derives each attribute in Metasploit::Model::Derivation::ClassMethods#validate_by_derived_attribute if the attribute is nil.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/metasploit/model/derivation.rb', line 100

def derive
  self.class.validate_by_derived_attribute.each_key do |attribute|
    value = send(attribute)

    # explicitly check for `nil` in case attribute is Boolean
    if value.nil?
      derived_value = send("derived_#{attribute}")
      send("#{attribute}=", derived_value)
    end
  end
end