class CodeCaser::Analyzer
This class scans the provided files to determine if any converted identifiers overlap with existing identifiers.
Public Class Methods
new(opts)
click to toggle source
# File lib/code_caser/analyzer.rb, line 6 def initialize(opts) @converter = opts.fetch(:converter) @files = PathConverter.new(opts.fetch(:path)).get_files @verbose = opts.fetch(:verbose, false) @existing_identifiers = {} @new_identifiers = {} @overlapping_identifiers = [] end
Public Instance Methods
analyze()
click to toggle source
# File lib/code_caser/analyzer.rb, line 15 def analyze load_existing_identifiers @files.each { |f| analyze_file(f) if File.file?(f) } puts @existing_identifiers.keys.sort.inspect print_new_identifiers if @verbose @overlapping_identifiers = @new_identifiers.select {|k,v| @existing_identifiers.key?(k) }.keys print_overlap end
analyze_file(file_path)
click to toggle source
# File lib/code_caser/analyzer.rb, line 37 def analyze_file(file_path) puts "loading file: #{file_path}" if @verbose IO.foreach(file_path) { |line| analyze_line(line) } end
load_existing_identifiers()
click to toggle source
# File lib/code_caser/analyzer.rb, line 24 def load_existing_identifiers @files.each { |f| load_existing_identifiers_from_file(f) if File.file?(f) } end
load_existing_identifiers_from_file(file_path)
click to toggle source
# File lib/code_caser/analyzer.rb, line 28 def load_existing_identifiers_from_file(file_path) puts "loading file: #{file_path}" if @verbose IO.foreach(file_path) { |line| load_existing_identifiers_from_line(line) } end
load_existing_identifiers_from_line(line)
click to toggle source
# File lib/code_caser/analyzer.rb, line 33 def load_existing_identifiers_from_line(line) split_line(line).each { |l| @existing_identifiers[l] = true } end
print_new_identifiers()
click to toggle source
# File lib/code_caser/analyzer.rb, line 51 def print_new_identifiers puts "\nThe following identifiers would be replaced:".colorize(:yellow) @new_identifiers.sort.to_h.each {|k,v| puts "#{v} -> #{k.colorize(:green)}" } end
print_overlap()
click to toggle source
# File lib/code_caser/analyzer.rb, line 42 def print_overlap if @overlapping_identifiers.empty? puts "\nNo overlapping identifiers found.".colorize(:green) else puts "\nThe following identifiers would overlap with existing names:".colorize(:yellow) puts @overlapping_identifiers.join(",").colorize(:yellow) end end
Private Instance Methods
analyze_line(original_line)
click to toggle source
# File lib/code_caser/analyzer.rb, line 58 def analyze_line(original_line) # discard anything after the ignore_after identifier original_identifiers = @converter.chop(original_line).split(/\W+/) original_identifiers.each do |identifier| if identifier != (new_identifier = @converter.convert_line(identifier)) @new_identifiers[new_identifier] = identifier end end end
split_line(line)
click to toggle source
# File lib/code_caser/analyzer.rb, line 68 def split_line(line) @converter.chop(line).split(/\W+/) end