Class: Reek::Configuration::ConfigurationConverter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ConfigurationValidator

#key_to_smell_detector, #smell_type?, #with_valid_directory

Constructor Details

#initialize(configuration) ⇒ ConfigurationConverter

Returns a new instance of ConfigurationConverter.

Parameters:

  • configuration (Hash)

    e.g.

    detectors =>

    "UnusedPrivateMethod" => {"exclude"=>["/exclude regexp/"],
    "UncommunicativeMethodName"=>name"], "accept"=>["accept name"]
    

    , directories =>

    "app/controllers" => {
      "UnusedPrivateMethod" => {"exclude"=>["/exclude regexp/"],
      "UncommunicativeMethodName"=>name"], "accept"=>["accept name"]
    }
    

    }



26
27
28
# File 'lib/reek/configuration/configuration_converter.rb', line 26

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (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

#convertHash

Converts all marked strings across the whole configuration to regexes.

Returns:

  • (Hash)


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”.

Parameters:

  • detector_configuration (Hash)

    e.g. { “UnusedPrivateMethod” => {“exclude”=>[“/exclude regexp/”] }

Returns:

  • (Array)

    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.

Parameters:

  • value (String)

    String that is potentially marked as regex, e.g. “/foobar/”.

Returns:

  • (Bool)

    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_detectorsObject (private)

Iterates over our detector configuration and converts all marked strings into regexes.

Returns:

  • nil



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_directoriesObject (private)

Iterates over our directory configuration and converts all marked strings into regexes.

Returns:

  • nil



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/.

Parameters:

  • value (value)

    String that is potentially marked as regex, e.g. “/foobar/”.

Returns:

  • (Regexp)

    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