Module: Reek::Configuration::DirectoryDirectives

Includes:
ConfigurationValidator
Defined in:
lib/reek/configuration/directory_directives.rb

Overview

Hash extension for directory directives.

Instance Method Summary collapse

Methods included from ConfigurationValidator

#key_to_smell_detector, #smell_type?, #with_valid_directory

Instance Method Details

#add(directory_config) ⇒ self

Adds a directive and returns self.

Parameters:

  • directory_config (Hash)

    the configuration e.g.:

    "samples/two_smelly_files" => {:IrresponsibleModule=>{:enabled=>false},
    "samples/three_clean_files" => :Attribute=>{:enabled=>true}
    

    }

Returns:

  • (self)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reek/configuration/directory_directives.rb', line 38

def add(directory_config)
  directory_config.each do |path, detector_config|
    with_valid_directory(path) do |directory|
      self[directory] = detector_config.each_with_object({}) do |(key, value), hash|
        abort(error_message_for_invalid_smell_type(key)) unless smell_type?(key)
        hash[key_to_smell_detector(key)] = value
      end
    end
  end
  self
end

#best_match_for(source_base_dir) ⇒ Object (private)



54
55
56
57
58
59
60
# File 'lib/reek/configuration/directory_directives.rb', line 54

def best_match_for(source_base_dir)
  keys.
    select do |pathname|
      match?(source_base_dir, pathname) || match?(source_base_dir, pathname.expand_path)
    end.
    max_by { |pathname| pathname.to_s.length }
end

#directive_for(source_via) ⇒ Hash | nil

Returns the directive for a given source.

Parameters:

  • source_via (String)

    the source of the code inspected

Returns:

  • (Hash | nil)

    the configuration for the source or nil



18
19
20
21
22
23
24
# File 'lib/reek/configuration/directory_directives.rb', line 18

def directive_for(source_via)
  return unless source_via

  source_base_dir = Pathname.new(source_via).dirname
  hit = best_match_for source_base_dir
  self[hit]
end

#error_message_for_invalid_smell_type(klass) ⇒ Object (private)



93
94
95
96
97
# File 'lib/reek/configuration/directory_directives.rb', line 93

def error_message_for_invalid_smell_type(klass)
  "You are trying to configure smell type #{klass} but we can't find one with that name.\n" \
    "Please make sure you spelled it right. (See 'docs/defaults.reek.yml' in the Reek\n" \
    'repository for a list of all available smell types.)'
end

#glob_to_regexp(glob) ⇒ Object (private)

Transform a glob pattern to a regexp.

It changes:

  • /** to .*,

  • ** to .*,

    • to [^/]*.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/reek/configuration/directory_directives.rb', line 75

def glob_to_regexp(glob)
  is_glob_pattern = glob.include?('*')

  regexp = if is_glob_pattern
             glob.
               gsub(%r{/\*\*$}, '<<to_eol_wildcards>>').
               gsub('**', '<<to_wildcards>>').
               gsub('*', '[^/]*').
               gsub('.', '\.').
               gsub('<<to_eol_wildcards>>', '.*').
               gsub('<<to_wildcards>>', '.*')
           else
             "#{glob}.*"
           end

  Regexp.new("^#{regexp}$", Regexp::IGNORECASE)
end

#match?(source_base_dir, pathname) ⇒ Boolean (private)

Returns:

  • (Boolean)


62
63
64
# File 'lib/reek/configuration/directory_directives.rb', line 62

def match?(source_base_dir, pathname)
  source_base_dir.to_s.match(glob_to_regexp(pathname.to_s))
end