Class: MetricFu::Reek

Inherits:
Generator show all
Defined in:
lib/generators/reek.rb

Constant Summary collapse

REEK_REGEX =
/^(\S+) (.*) \((.*)\)$/

Instance Attribute Summary

Attributes inherited from Generator

#report, #template

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

class_name, #create_data_dir_if_missing, #create_metric_dir_if_missing, #create_output_dir_if_missing, generate_report, #generate_report, #initialize, #metric_directory, metric_directory, #remove_excluded_files, #round_to_tenths, #to_graph

Constructor Details

This class inherits a constructor from MetricFu::Generator

Class Method Details

.verify_dependencies!Object



6
7
8
9
# File 'lib/generators/reek.rb', line 6

def self.verify_dependencies!
  `reek --help`
  raise 'sudo gem install reek # if you want the reek tasks' unless $?.success?
end

Instance Method Details

#analyzeObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/reek.rb', line 43

def analyze
  @matches = @output.chomp.split("\n\n").map{|m| m.split("\n") }
  @matches = @matches.map do |match|
    file_path = match.shift.split('--').first
    file_path = file_path.gsub('"', ' ').strip
    code_smells = match.map do |smell|
      match_object = smell.match(REEK_REGEX)
      next unless match_object
      {:method => match_object[1].strip,
       :message => match_object[2].strip,
       :type => match_object[3].strip}
    end.compact
    {:file_path => file_path, :code_smells => code_smells}
  end
end

#emitObject



11
12
13
14
15
16
# File 'lib/generators/reek.rb', line 11

def emit
  files_to_reek = MetricFu.reek[:dirs_to_reek].map{|dir| Dir[File.join(dir, "**/*.rb")] }
  files = remove_excluded_files(files_to_reek.flatten)
  @output = `reek #{files.join(" ")}`
  @output = massage_for_reek_12 if reek_12?
end

#massage_for_reek_12Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/reek.rb', line 23

def massage_for_reek_12
  section_break = ''
  @output.split("\n").map do |line|
    case line
    when /^  /
      "#{line.gsub(/^  /, '')}\n"
    else
      parts = line.split(" -- ")
      if parts[1].nil?
        "#{line}\n"
      else
        warnings = parts[1].gsub(/ \(.*\):/, ':')
        result = "#{section_break}\"#{parts[0]}\" -- #{warnings}\n"
        section_break = "\n"
        result
      end
    end
  end.join
end

#reek_12?Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/generators/reek.rb', line 18

def reek_12?
  return false if @output.length == 0
  (@output =~ /^"/) != 0
end

#to_hObject



59
60
61
# File 'lib/generators/reek.rb', line 59

def to_h
  {:reek => {:matches => @matches}}
end