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(config, mute: false) ⇒ D2Checker

Returns a new instance of D2Checker.

Parameters:

  • config (D2Config)
  • mute (Boolean) (defaults to: false)


9
10
11
12
# File 'lib/distincter2/checker.rb', line 9

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

Instance Method Details

#analyze_dir(path) ⇒ String[]

rubocop:disable Metrics/MethodLength

Parameters:

  • path (String)

Returns:

  • (String[])


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/distincter2/checker.rb', line 24

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

    entry_name = ::File.basename(entry)

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

      next if @config.exclude_paths.include?(entry_name)

      analyze_result = analyze_file(file)
    end

    duplicates += analyze_result unless analyze_result.empty?
  end

  duplicates
end

#analyze_file(path) ⇒ String[]

rubocop:disable Metrics/MethodLength

Parameters:

  • path (String)

Returns:

  • (String[])


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/distincter2/checker.rb', line 53

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

Parameters:

  • path (String)


15
16
17
18
19
# File 'lib/distincter2/checker.rb', line 15

def check(path)
  duplicates = analyze_dir(path)

  exit(duplicates.empty? ? 0 : 1)
end