module MetasploitDataModels::Match::Child

Adds a {#match match class method} to the extending class. The extending class must define `MATCH_REGEXP`.

@example Define `match` class method

class MetasploitDataModels::Format
  extend MetasploitDataModels::Match::Child

  #
  # CONSTANTS
  #

  # Regular expression {MetasploitDataModels::Match#match} must match against.
  MATCH_REGEXP = /\A...\z/
end

# a `MetasploitDataModels::Format` because `'123'` matches `MetasploitDataModels::Format::MATCH_REGEXP`
instance = MetapsloitDataModels::Format.match('123')
# `nil` because string `'12'` doesn't match `MetasploitDataModels::Format::MATCH_REGEXP`
no_instance = MetasploitDataModels::Format.match('12')

Public Instance Methods

match(formatted_value) click to toggle source

Creates a new instance of the extending class if `MATCH_REGEXP`, defined on the extending class, matches `formatted_value`.

@param formatted_value [#to_s]

# File lib/metasploit_data_models/match/child.rb, line 25
def match(formatted_value)
  instance = nil

  if match_regexp.match(formatted_value)
    instance = new(value: formatted_value)
  end

  instance
end
match_regexp() click to toggle source

Regular expression to match against for {#match}.

@return [Regexp] Defaults to {#regexp} pinned with `A` and `z`.

# File lib/metasploit_data_models/match/child.rb, line 38
def match_regexp
  @match_regexp ||= /\A#{regexp}\z/
end
regexp() click to toggle source

Regular expression to match child as part of {MetasploitDataModels::Match::Parent}.

@return [Regexp] Default to `REGEXP` from the extending `Class`.

# File lib/metasploit_data_models/match/child.rb, line 45
def regexp
  self::REGEXP
end