Class: Codeowners::Checker::LineGrouper

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

Overview

Create groups and subgroups structure for the lines in the CODEOWNERS file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, lines) ⇒ LineGrouper

Returns a new instance of LineGrouper.



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

def initialize(group, lines)
  @group_buffer = [group]
  @lines = lines
end

Class Method Details

.call(group, lines) ⇒ Object



7
8
9
# File 'lib/codeowners/checker/line_grouper.rb', line 7

def self.call(group, lines)
  new(group, lines).call
end

Instance Method Details

#callObject

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/codeowners/checker/line_grouper.rb', line 19

def call
  lines.each_with_index do |line, index|
    case line
    when Codeowners::Checker::Group::Empty
      ensure_groups_structure
    when Codeowners::Checker::Group::GroupBeginComment
      trim_groups(line.level)
      create_groups_structure(line.level)
    when Codeowners::Checker::Group::GroupEndComment
      trim_subgroups(line.level)
      create_groups_structure(line.level)
    when Codeowners::Checker::Group::Comment
      if previous_line_empty?(index)
        trim_groups(line.level)
      else
        trim_subgroups(line.level)
      end
      create_groups_structure(line.level)
    when Codeowners::Checker::Group::Pattern
      if new_owner?(line, index)
        trim_groups(current_level)
        new_group
      end
      ensure_groups_structure
    when Codeowners::Checker::Group::UnrecognizedLine
      ensure_groups_structure
    else
      raise "Do not know how to handle line: #{line.inspect}"
    end
    current_group.add(line)
  end
  group_buffer.first
end