class TrickBag::Validations::RegexStringListAnalyzer

Analyzes a list of strings and a list of regexes, gathering information about which regexes match which strings. The to_h method returns a hash whose keys are the regexes and values are arrays of strings in the string list that match the regex. Note that if a string matches multiple regexes, it will be added to the arrays of all the regexes it matched.

Attributes

regex_strings_hash[R]

Public Class Methods

new(regexes, strings) click to toggle source
# File lib/trick_bag/validations/regex_validations.rb, line 40
def initialize(regexes, strings)
  @regex_strings_hash = regexes.each_with_object({}) do |regex, match_hash|
    match_hash[regex] = []
  end
  strings.each do |string|
    regexes_matched = regexes.select { |regex| regex === string }
    regexes_matched.each do |regex_matched|
      regex_strings_hash[regex_matched] << string
    end
  end
end

Public Instance Methods

regexes_with_matches() click to toggle source

Takes a match hash returned by the match_hash method above, and returns the regexes for which matches were found.

# File lib/trick_bag/validations/regex_validations.rb, line 69
def regexes_with_matches
  regex_strings_hash.keys.reject { |key| regex_strings_hash[key].empty? }
end
regexes_with_matches_as_strings() click to toggle source

Takes a match hash returned by the match_hash method above, and returns string representations of the regexes for which matches were found.

# File lib/trick_bag/validations/regex_validations.rb, line 85
def regexes_with_matches_as_strings
  regexes_with_matches.map(&:inspect)
end
regexes_without_matches() click to toggle source

Takes a match hash returned by the match_hash method above, and returns the regexes for which no matches were found.

# File lib/trick_bag/validations/regex_validations.rb, line 62
def regexes_without_matches
  regex_strings_hash.keys.select { |key| regex_strings_hash[key].empty? }
end
regexes_without_matches_as_strings() click to toggle source

Takes a match hash returned by the match_hash method above, and returns string representations of the regexes for which no matches were found.

# File lib/trick_bag/validations/regex_validations.rb, line 77
def regexes_without_matches_as_strings
  regexes_without_matches.map(&:inspect)
end
to_h() click to toggle source

Returns a hash whose keys are the regexes, and the values are the strings that matched the regex key.

# File lib/trick_bag/validations/regex_validations.rb, line 55
def to_h
  @regex_strings_hash
end