Class: Reek::Configuration::ConfigurationConverter
- Inherits:
-
Object
- Object
- Reek::Configuration::ConfigurationConverter
- Includes:
- ConfigurationValidator
- Defined in:
- lib/reek/configuration/configuration_converter.rb
Overview
Responsible for converting marked strings coming from the outside world into proper regexes.
Constant Summary collapse
- REGEXABLE_ATTRIBUTES =
%w(accept reject exclude).freeze
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
-
#convert ⇒ Hash
Converts all marked strings across the whole configuration to regexes.
-
#convertible_attributes(detector_configuration) ⇒ Array
private
All the attributes from the detector configuration that potentially contain regexes.
-
#initialize(configuration) ⇒ ConfigurationConverter
constructor
A new instance of ConfigurationConverter.
-
#marked_as_regex?(value) ⇒ Bool
private
If the string in question is marked as regex.
-
#strings_to_regexes_for_detectors ⇒ Object
private
Iterates over our detector configuration and converts all marked strings into regexes.
-
#strings_to_regexes_for_directories ⇒ Object
private
Iterates over our directory configuration and converts all marked strings into regexes.
-
#to_regex(value) ⇒ Regexp
private
E.g.
Methods included from ConfigurationValidator
#key_to_smell_detector, #smell_type?, #with_valid_directory
Constructor Details
#initialize(configuration) ⇒ ConfigurationConverter
Returns a new instance of ConfigurationConverter.
26 27 28 |
# File 'lib/reek/configuration/configuration_converter.rb', line 26 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
12 13 14 |
# File 'lib/reek/configuration/configuration_converter.rb', line 12 def configuration @configuration end |
Instance Method Details
#convert ⇒ Hash
Converts all marked strings across the whole configuration to regexes.
33 34 35 36 37 38 |
# File 'lib/reek/configuration/configuration_converter.rb', line 33 def convert strings_to_regexes_for_detectors strings_to_regexes_for_directories configuration end |
#convertible_attributes(detector_configuration) ⇒ Array (private)
Returns all the attributes from the detector configuration that potentially contain regexes. Using this example above this would just be “exclude”.
63 64 65 |
# File 'lib/reek/configuration/configuration_converter.rb', line 63 def convertible_attributes(detector_configuration) detector_configuration.keys & REGEXABLE_ATTRIBUTES end |
#marked_as_regex?(value) ⇒ Bool (private)
Returns if the string in question is marked as regex.
46 47 48 |
# File 'lib/reek/configuration/configuration_converter.rb', line 46 def marked_as_regex?(value) value.start_with?('/') && value.end_with?('/') end |
#strings_to_regexes_for_detectors ⇒ Object (private)
Iterates over our detector configuration and converts all marked strings into regexes.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/reek/configuration/configuration_converter.rb', line 73 def strings_to_regexes_for_detectors return unless configuration[DETECTORS_KEY] configuration[DETECTORS_KEY].tap do |detectors| detectors.each_key do |detector| convertible_attributes(detectors[detector]).each do |attribute| detectors[detector][attribute] = detectors[detector][attribute].map do |item| to_regex item end end end end end |
#strings_to_regexes_for_directories ⇒ Object (private)
Iterates over our directory configuration and converts all marked strings into regexes.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/reek/configuration/configuration_converter.rb', line 93 def strings_to_regexes_for_directories return unless configuration[DIRECTORIES_KEY] configuration[DIRECTORIES_KEY].tap do |directories| directories.each_key do |directory| directories[directory].each do |detector, configuration| convertible_attributes(configuration).each do |attribute| directories[directory][detector][attribute] = directories[directory][detector][attribute].map do |item| to_regex item end end end end end end |
#to_regex(value) ⇒ Regexp (private)
Returns e.g. /foobar/.
53 54 55 |
# File 'lib/reek/configuration/configuration_converter.rb', line 53 def to_regex(value) marked_as_regex?(value) ? Regexp.new(value[1..-2]) : value end |