Class: Corser::Parser::Ruby
- Inherits:
-
Object
- Object
- Corser::Parser::Ruby
- Defined in:
- lib/corser.rb
Constant Summary collapse
- RUBY_VER =
2.3
- KEYWORDS_LIST =
%w(__ENCODING__ __LINE__ __FILE__ BEGIN END alias and begin break case defined? do else elsif ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield )
- CSV_ROW_LIST =
%w{size_of_all_keywords tenary_count if_else_deviation comments_size function_count avg_depth_of_lines standart_line_deviation single_quotes_size double_quotes_size same_developer}
Instance Attribute Summary collapse
-
#all_lines_size ⇒ Object
readonly
Returns the value of attribute all_lines_size.
-
#parsed_data ⇒ Object
readonly
Returns the value of attribute parsed_data.
-
#raw_code ⇒ Object
readonly
Returns the value of attribute raw_code.
Instance Method Summary collapse
- #all_empty_lines_size ⇒ Object
- #avg_all_lines ⇒ Object
- #avg_depth_of_lines ⇒ Object
- #comments_size ⇒ Object
- #depth_of_lines ⇒ Object
- #double_quotes_size ⇒ Object
- #empty_lines_size ⇒ Object
- #function_count ⇒ Object
- #if_else_deviation ⇒ Object
-
#initialize(file_path) ⇒ Ruby
constructor
A new instance of Ruby.
- #max_depth_of_lines ⇒ Object
- #save_to_csv(same_developer = 1, *methods_list, file_path) ⇒ Object
- #single_quotes_size ⇒ Object
- #size_of_all_keywords ⇒ Object
- #size_of_each_keyword ⇒ Object
- #standart_line_deviation ⇒ Object
- #tenary_count ⇒ Object
Constructor Details
#initialize(file_path) ⇒ Ruby
Returns a new instance of Ruby.
22 23 24 25 26 |
# File 'lib/corser.rb', line 22 def initialize(file_path) @parsed_data = RuboCop::ProcessedSource.send(:from_file, file_path, RUBY_VER) @raw_code = truncate_comments(parsed_data.raw_source, parsed_data.comments) @all_lines_size = parsed_data.lines.size end |
Instance Attribute Details
#all_lines_size ⇒ Object (readonly)
Returns the value of attribute all_lines_size.
9 10 11 |
# File 'lib/corser.rb', line 9 def all_lines_size @all_lines_size end |
#parsed_data ⇒ Object (readonly)
Returns the value of attribute parsed_data.
9 10 11 |
# File 'lib/corser.rb', line 9 def parsed_data @parsed_data end |
#raw_code ⇒ Object (readonly)
Returns the value of attribute raw_code.
9 10 11 |
# File 'lib/corser.rb', line 9 def raw_code @raw_code end |
Instance Method Details
#all_empty_lines_size ⇒ Object
92 93 94 |
# File 'lib/corser.rb', line 92 def all_empty_lines_size (parsed_data.lines.select { |l| l.empty? }.size - 1).to_f / all_lines_size end |
#avg_all_lines ⇒ Object
69 70 71 |
# File 'lib/corser.rb', line 69 def avg_all_lines (parsed_data.lines.map(&:length).sum - comments_length).to_f / (all_lines_size - comments_size) end |
#avg_depth_of_lines ⇒ Object
108 109 110 |
# File 'lib/corser.rb', line 108 def avg_depth_of_lines depth_of_lines.instance_eval { reduce(:+) / size.to_f } end |
#comments_size ⇒ Object
65 66 67 |
# File 'lib/corser.rb', line 65 def comments_size parsed_data.comments.size.to_f / all_lines_size end |
#depth_of_lines ⇒ Object
100 101 102 |
# File 'lib/corser.rb', line 100 def depth_of_lines parsed_data.lines.map { |l| l.scan(/^\s*/) }.flatten.map(&:length) end |
#double_quotes_size ⇒ Object
116 117 118 |
# File 'lib/corser.rb', line 116 def double_quotes_size (raw_code.scan(/".*"/).size.to_f / avg_all_lines).to_f end |
#empty_lines_size ⇒ Object
88 89 90 |
# File 'lib/corser.rb', line 88 def empty_lines_size parsed_data.ast&.each_node(:def, :defs).map { |m| m.source.scan(/^[\s]*?$\n/).size.to_f } end |
#function_count ⇒ Object
73 74 75 |
# File 'lib/corser.rb', line 73 def function_count parsed_data.ast&.each_node(:def, :defs).count.to_f / all_lines_size end |
#if_else_deviation ⇒ Object
81 82 83 84 85 86 |
# File 'lib/corser.rb', line 81 def if_else_deviation ifs = raw_code.scan(/\bif\b/).size elses = raw_code.scan(/\belse\b/).size return 0.00 if ifs == 0 (elses / ifs).to_f end |
#max_depth_of_lines ⇒ Object
104 105 106 |
# File 'lib/corser.rb', line 104 def max_depth_of_lines depth_of_lines.max end |
#save_to_csv(same_developer = 1, *methods_list, file_path) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/corser.rb', line 28 def save_to_csv(same_developer = 1, *methods_list, file_path) if methods_list.empty? methods_list = CSV_ROW_LIST else methods_list << "same_developer" end data = [] methods_list.each do |method| data << eval(method).round(2) end CSV.open(file_path, "ab") { |csv| csv << data } if same_developer == 1 pick = pick_csv_line("species/dataset.csv") CSV.open(file_path, "ab") { |csv| csv << pick } end end |
#single_quotes_size ⇒ Object
112 113 114 |
# File 'lib/corser.rb', line 112 def single_quotes_size (raw_code.scan(/'.*'/).size.to_f / avg_all_lines).to_f end |
#size_of_all_keywords ⇒ Object
49 50 51 |
# File 'lib/corser.rb', line 49 def size_of_all_keywords raw_code.scan(/\b#{KEYWORDS_LIST.join('\b|\b')}\b/).size.to_f / all_lines_size end |
#size_of_each_keyword ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/corser.rb', line 53 def size_of_each_keyword sum = 0 word_counter = 0 KEYWORDS_LIST.each do |keyword| word_counter = raw_code.scan(/(\b(#{keyword})\b)/).size.to_f sum = sum + word_counter p "#{keyword}: #{word_counter}" if word_counter > 0 end sum end |
#standart_line_deviation ⇒ Object
96 97 98 |
# File 'lib/corser.rb', line 96 def standart_line_deviation (raw_code.lines.map(&:length).max - 1).to_f / avg_all_lines end |
#tenary_count ⇒ Object
77 78 79 |
# File 'lib/corser.rb', line 77 def tenary_count raw_code.scan(/\s\?|\bif\b/).size.to_f / all_lines_size end |