Class: Distincter2::D2Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/distincter2/checker.rb

Overview

distincter2 checker. Handles a file and operate them.

Instance Method Summary collapse

Constructor Details

#initialize(mute: false) ⇒ D2Checker

Mute if you don’t want to see errors in output



6
7
8
# File 'lib/distincter2/checker.rb', line 6

def initialize(mute: false)
  @mute = mute
end

Instance Method Details

#analyze_dir(path) ⇒ Object

Analyze given directory with recursion. rubocop:disable Metrics/MethodLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/distincter2/checker.rb', line 18

def analyze_dir(path)
  duplicates = []
  ::Dir.foreach(path).each do |entry|
    next if entry.start_with?('.')

    file = "#{::File.absolute_path(path)}/#{::File.basename(entry)}"
    analyze_result = []
    if ::File.directory?(file)
      analyze_result = analyze_dir(file)
    else
      next unless entry.end_with?('.md')

      analyze_result = analyze_file(file)
    end
    duplicates += analyze_result unless analyze_result.empty?
  end
  duplicates
end

#analyze_file(path) ⇒ Object

Analyze given file. rubocop:disable Metrics/MethodLength



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/distincter2/checker.rb', line 41

def analyze_file(path)
  lines = []
  ::File.open(path, 'r') do |file|
    file.each_line do |line|
      lines << line if !line.empty? && line.start_with?('-')
    end
  end
  duplicates = lines.select { |line| lines.count(line) > 1 }
                    .uniq
  unless @mute
    duplicates.each do |duplicate|
      puts("#{path} : #{duplicate}")
    end
  end
  duplicates
end

#check(path) ⇒ Object

Check entrypoint.



11
12
13
14
# File 'lib/distincter2/checker.rb', line 11

def check(path)
  duplicates = analyze_dir(path)
  exit(duplicates.empty? ? 0 : 1)
end