Class: Precheck::AbstractTextMatchRule

Inherits:
TextRule show all
Defined in:
precheck/lib/precheck/rules/abstract_text_match_rule.rb

Overview

abstract class that defines a default way to check for the presence of a list of words within a block of text

Constant Summary collapse

WORD_SEARCH_TYPES =
{
  fail_on_inclusion: "fail_on_inclusion",
  fail_on_exclusion: "fail_on_exclusion"
}

Instance Attribute Summary collapse

Attributes inherited from FastlaneCore::ConfigItem

#allow_shell_conversion, #code_gen_default_value, #code_gen_sensitive, #conflict_block, #conflicting_options, #default_value, #default_value_dynamic, #deprecated, #description, #display_in_shell, #env_name, #env_names, #key, #optional, #sensitive, #short_option, #skip_type_validation, #verify_block

Instance Method Summary collapse

Methods inherited from TextRule

#handle_item?

Methods inherited from Rule

#check_item, #customize_with_data, default_value, description, env_name, friendly_name, #friendly_name, #handle_item?, #initialize, #inspect, #item_field_supported?, key, #needs_customization?, #perform_check, #skip_item_not_meant_for_this_rule, #supported_fields_symbol_set, #to_s

Methods inherited from FastlaneCore::ConfigItem

#auto_convert_value, #data_type, #deprecated_description, #doc_default_value, #ensure_array_type_passes_validation, #ensure_boolean_type_passes_validation, #ensure_generic_type_passes_validation, #fetch_env_value, #help_default_value, #initialize, #is_string, #string?, #to_s, #update_code_gen_default_value_if_able!, #valid?, #verify!

Constructor Details

This class inherits a constructor from Precheck::Rule

Instance Attribute Details

#lowercased_words_to_look_forObject

Returns the value of attribute lowercased_words_to_look_for.



11
12
13
# File 'precheck/lib/precheck/rules/abstract_text_match_rule.rb', line 11

def lowercased_words_to_look_for
  @lowercased_words_to_look_for
end

Instance Method Details

#allowed_lowercased_wordsObject

list of words or phrases that should be excluded from this rule they will be removed from the text string before the rule is executed



19
20
21
# File 'precheck/lib/precheck/rules/abstract_text_match_rule.rb', line 19

def allowed_lowercased_words
  []
end

#pass_if_empty?Boolean

Returns:



23
24
25
# File 'precheck/lib/precheck/rules/abstract_text_match_rule.rb', line 23

def pass_if_empty?
  return true
end

#remove_safe_words(text: nil) ⇒ Object



31
32
33
34
35
36
37
# File 'precheck/lib/precheck/rules/abstract_text_match_rule.rb', line 31

def remove_safe_words(text: nil)
  text_without_safe_words = text
  allowed_lowercased_words.each do |safe_word|
    text_without_safe_words.gsub!(safe_word, '')
  end
  return text_without_safe_words
end

#rule_blockObject

rule block that checks text for any instance of each string in lowercased_words_to_look_for



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'precheck/lib/precheck/rules/abstract_text_match_rule.rb', line 40

def rule_block
  return lambda { |text|
    text = text.to_s.strip.downcase
    if text.empty?
      if pass_if_empty?
        return RuleReturn.new(validation_state: Precheck::VALIDATION_STATES[:passed])
      else
        return RuleReturn.new(validation_state: VALIDATION_STATES[:failed], failure_data: "missing text")
      end
    end

    text = remove_safe_words(text: text)

    matches = lowercased_words_to_look_for.each_with_object([]) do |word, found_words|
      if text.include?(word)
        found_words << word
      end
    end

    if matches.length > 0 && word_search_type == WORD_SEARCH_TYPES[:fail_on_inclusion]
      # we are supposed to fail if any of the words are found
      friendly_matches = matches.join(', ')
      UI.verbose("😭  #{self.class.name.split('::').last ||= self.class.name} found words \"#{friendly_matches}\"")

      return RuleReturn.new(validation_state: VALIDATION_STATES[:failed], failure_data: "found: #{friendly_matches}")
    elsif matches.length < lowercased_words_to_look_for.length && word_search_type == WORD_SEARCH_TYPES[:fail_on_exclusion]
      # we are supposed to fail if any of the words are not found (like current copyright date in the copyright field)
      search_data_set = lowercased_words_to_look_for.to_set
      search_data_set.subtract(matches)

      missing_words = search_data_set.to_a.join(', ')
      UI.verbose("😭  #{self.class.name.split('::').last ||= self.class.name} didn't find words \"#{missing_words}\"")

      return RuleReturn.new(validation_state: VALIDATION_STATES[:failed], failure_data: "missing: #{missing_words}")
    else
      return RuleReturn.new(validation_state: VALIDATION_STATES[:passed])
    end
  }
end

#word_search_typeObject



27
28
29
# File 'precheck/lib/precheck/rules/abstract_text_match_rule.rb', line 27

def word_search_type
  WORD_SEARCH_TYPES[:fail_on_inclusion]
end