Sha256: d85ed83d4766f2d298dbcd7ec6c1d8cfe8411be1e8b0060db1f106c48dcc591e

Contents?: true

Size: 1.46 KB

Versions: 74

Compression:

Stored size: 1.46 KB

Contents

# 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')
#
module MetasploitDataModels::Match::Child
  # Creates a new instance of the extending class if `MATCH_REGEXP`, defined on the extending class, matches
  # `formatted_value`.
  #
  # @param formatted_value [#to_s]
  def match(formatted_value)
    instance = nil

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

    instance
  end

  # Regular expression to match against for {#match}.
  #
  # @return [Regexp] Defaults to {#regexp} pinned with `\A` and `\z`.
  def match_regexp
    @match_regexp ||= /\A#{regexp}\z/
  end

  # Regular expression to match child as part of {MetasploitDataModels::Match::Parent}.
  #
  # @return [Regexp] Default to `REGEXP` from the extending `Class`.
  def regexp
    self::REGEXP
  end
end

Version data entries

74 entries across 74 versions & 2 rubygems

Version Path
metasploit_data_models-2.0.9 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.8 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.7 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.6 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.5 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.4 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.1 lib/metasploit_data_models/match/child.rb
metasploit_data_models-2.0.0 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.3.0 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.11 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.10 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.9 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.8 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.7 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.6 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.5 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.4 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.3 lib/metasploit_data_models/match/child.rb
metasploit_data_models-1.2.2 lib/metasploit_data_models/match/child.rb
metasploit_data_models-0.24.5 lib/metasploit_data_models/match/child.rb