Class: MetricFu::Saikuro::SFile

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/saikuro.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SFile

Returns a new instance of SFile.


98
99
100
101
# File 'lib/generators/saikuro.rb', line 98

def initialize(path)
  @path = path
  set_elements
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements


96
97
98
# File 'lib/generators/saikuro.rb', line 96

def elements
  @elements
end

Class Method Details

.is_valid_text_file?(path) ⇒ Boolean

Returns:

  • (Boolean)

103
104
105
106
107
108
109
110
111
# File 'lib/generators/saikuro.rb', line 103

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

Instance Method Details

#filenameObject


113
114
115
# File 'lib/generators/saikuro.rb', line 113

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

#get_class_namesObject


174
175
176
177
178
179
180
181
182
# File 'lib/generators/saikuro.rb', line 174

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

#merge_classesObject


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/generators/saikuro.rb', line 148

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 {|d| d.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

#set_elementsObject


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/generators/saikuro.rb', line 122

def set_elements
  @elements = []
  File.open(@path, "r") do |file|
    while (line = file.readline) do
      return [] if line.nil? || line !~ /\S/
      element ||= nil
      if line.match /START/
        unless element.nil?
          @elements << element
          element = nil
        end
        line = file.readline
        element = Saikuro::ParsingElement.new(line)
      elsif line.match /END/
        @elements << element if element
        element = nil
      else
        element << line if element
      end
    end
  end
rescue EOFError
  nil
end

#to_hObject


117
118
119
120
# File 'lib/generators/saikuro.rb', line 117

def to_h
  merge_classes
  {:classes => @elements}
end