Class: MetricFu::SaikuroScratchFile

Inherits:
Object
  • Object
show all
Defined in:
lib/metric_fu/metrics/saikuro/scratch_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SaikuroScratchFile

Returns a new instance of SaikuroScratchFile.



19
20
21
22
23
24
25
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 19

def initialize(path)
  @path = path
  @elements = []
  File.open(@path, "rb") do |file|
    get_elements(file)
  end
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



17
18
19
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 17

def elements
  @elements
end

Class Method Details

.assemble_files(glob) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 4

def self.assemble_files(glob)
  files = []
  glob.each do |path|
    if is_valid_text_file?(path)
      file = new(path)
      if file
        files << file
      end
    end
  end
  files
end

.is_valid_text_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 27

def self.is_valid_text_file?(path)
  File.open(path, "rb") do |file|
    if file.eof? || !file.readline.match(/--/)
      return false
    else
      return true
    end
  end
end

Instance Method Details

#filenameObject



37
38
39
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 37

def filename
  File.basename(@path, "_cyclo.html")
end

#filepathObject



41
42
43
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 41

def filepath
  File.dirname(@path)
end

#get_class_namesObject



98
99
100
101
102
103
104
105
106
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 98

def get_class_names
  class_names = []
  @elements.each do |element|
    unless class_names.include?(element.name)
      class_names << element.name
    end
  end
  class_names
end

#get_elements(io) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 50

def get_elements(io)
  while (line = io.readline)
    return [] if line.nil? || line !~ /\S/
    element ||= nil
    if line.match(/START/)
      unless element.nil?
        @elements << element
        element = nil
      end
      line = io.readline
      element = MetricFu::SaikuroParsingElement.new(line)
    elsif line.match(/END/)
      @elements << element if element
      element = nil
    else
      element << line if element
    end
  end
rescue EOFError
  nil
end

#merge_classesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 72

def merge_classes
  new_elements = []
  get_class_names.each do |target_class|
    elements = @elements.find_all { |el| el.name == target_class }
    complexity = 0
    lines = 0
    defns = []
    elements.each do |el|
      complexity += el.complexity.to_i
      lines += el.lines.to_i
      defns << el.defs
    end

    new_element = { class_name: target_class,
                    complexity: complexity,
                    lines: lines,
                    methods: defns.flatten.map(&:to_h) }
    new_element[:methods] = new_element[:methods].
                            sort_by { |x| x[:complexity] }.
                            reverse

    new_elements << new_element
  end
  @elements = new_elements if new_elements
end

#to_hObject



45
46
47
48
# File 'lib/metric_fu/metrics/saikuro/scratch_file.rb', line 45

def to_h
  merge_classes
  { classes: @elements }
end