Class: TrickBag::Validations::RegexStringListAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/trick_bag/validations/regex_validations.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexes, strings) ⇒ RegexStringListAnalyzer

Returns a new instance of RegexStringListAnalyzer.



40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Attribute Details

#regex_strings_hashObject (readonly)

Returns the value of attribute regex_strings_hash.



38
39
40
# File 'lib/trick_bag/validations/regex_validations.rb', line 38

def regex_strings_hash
  @regex_strings_hash
end

Instance Method Details

#regexes_with_matchesObject

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



69
70
71
# 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_stringsObject

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



85
86
87
# File 'lib/trick_bag/validations/regex_validations.rb', line 85

def regexes_with_matches_as_strings
  regexes_with_matches.map(&:inspect)
end

#regexes_without_matchesObject

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



62
63
64
# 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_stringsObject

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



77
78
79
# File 'lib/trick_bag/validations/regex_validations.rb', line 77

def regexes_without_matches_as_strings
  regexes_without_matches.map(&:inspect)
end

#to_hObject

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



55
56
57
# File 'lib/trick_bag/validations/regex_validations.rb', line 55

def to_h
  @regex_strings_hash
end