class Codeowners::Checker::Group

Manage the groups content and handle operations on the groups.

Attributes

list[RW]
parent[RW]

Public Class Methods

new() click to toggle source
# File lib/codeowners/checker/group.rb, line 19
def initialize
  @list = []
end
parse(lines) click to toggle source
# File lib/codeowners/checker/group.rb, line 15
def self.parse(lines)
  new.parse(lines)
end

Public Instance Methods

==(other) click to toggle source
# File lib/codeowners/checker/group.rb, line 120
def ==(other)
  other.is_a?(Group) && other.list == list
end
add(line) click to toggle source
# File lib/codeowners/checker/group.rb, line 98
def add(line)
  line.parent = self
  @list << line
end
create_subgroup() click to toggle source
# File lib/codeowners/checker/group.rb, line 91
def create_subgroup
  group = self.class.new
  group.parent = self
  @list << group
  group
end
each() { |object| ... } click to toggle source
# File lib/codeowners/checker/group.rb, line 23
def each(&block)
  @list.each do |object|
    if object.is_a?(Group)
      object.each(&block)
    else
      yield(object)
    end
  end
end
insert(line) click to toggle source
# File lib/codeowners/checker/group.rb, line 103
def insert(line)
  line.parent = self
  index = insert_at_index(line)
  @list.insert(index, line)
end
owner() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/codeowners/checker/group.rb, line 65
def owner
  owners.first
end
owners() click to toggle source

Owners are ordered by the amount of occurrences

# File lib/codeowners/checker/group.rb, line 70
def owners
  all_owners.group_by(&:itself).sort_by do |_owner, occurrences|
    -occurrences.count
  end.map(&:first)
end
parse(lines) click to toggle source
# File lib/codeowners/checker/group.rb, line 33
def parse(lines)
  LineGrouper.call(self, lines)
end
remove(line) click to toggle source
# File lib/codeowners/checker/group.rb, line 109
def remove(line)
  @list.safe_delete(line)
  remove! unless any? { |object| object.is_a? Pattern }
end
remove!() click to toggle source
# File lib/codeowners/checker/group.rb, line 114
def remove!
  @list.clear
  parent&.remove(self)
  self.parent = nil
end
subgroups_owned_by(owner) click to toggle source
# File lib/codeowners/checker/group.rb, line 76
def subgroups_owned_by(owner)
  @list.flat_map do |item|
    next unless item.is_a?(Group)

    a = []
    a << item if item.owner == owner
    a += item.subgroups_owned_by(owner)
    a
  end.compact
end
title() click to toggle source
# File lib/codeowners/checker/group.rb, line 87
def title
  @list.first.to_s
end
to_content() click to toggle source
# File lib/codeowners/checker/group.rb, line 37
def to_content
  @list.flat_map(&:to_content)
end
to_file() click to toggle source
# File lib/codeowners/checker/group.rb, line 41
def to_file
  @list.flat_map(&:to_file)
end
to_tree(indentation = '') click to toggle source

Returns an array of strings representing the structure of the group. It indent internal subgroups for readability and debugging purposes. rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/codeowners/checker/group.rb, line 49
def to_tree(indentation = '')
  @list.each_with_index.flat_map do |item, index|
    if indentation.empty?
      item.to_tree(indentation + ' ')
    elsif index.zero?
      item.to_tree(indentation + '+ ')
    elsif index == @list.length - 1
      item.to_tree(indentation + '\\ ')
    else
      item.to_tree(indentation + '| ')
    end
  end
end

Private Instance Methods

all_owners() click to toggle source
# File lib/codeowners/checker/group.rb, line 130
def all_owners
  flat_map do |item|
    item.owners if item.pattern?
  end.compact
end
find_last_line_of_initial_comments() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/codeowners/checker/group.rb, line 152
def find_last_line_of_initial_comments
  @list.each_with_index do |item, index|
    return index unless item.is_a?(Comment)
  end
  0
end
insert_at_index(line) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/codeowners/checker/group.rb, line 137
def insert_at_index(line)
  new_patterns_sorted = @list.grep(Pattern).dup.push(line).sort
  previous_line_index = new_patterns_sorted.index { |l| l.equal? line } - 1
  previous_line = new_patterns_sorted[previous_line_index]
  padding = previous_line.pattern.size + previous_line.whitespace - line.pattern.size
  line.whitespace = [1, padding].max

  if previous_line_index >= 0
    @list.index { |l| l.equal? previous_line } + 1
  else
    find_last_line_of_initial_comments
  end
end