Class: Codeowners::Checker::Group

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/codeowners/checker/group.rb,
lib/codeowners/checker/group/line.rb,
lib/codeowners/checker/group/empty.rb,
lib/codeowners/checker/group/comment.rb,
lib/codeowners/checker/group/pattern.rb,
lib/codeowners/checker/group/group_end_comment.rb,
lib/codeowners/checker/group/unrecognized_line.rb,
lib/codeowners/checker/group/group_begin_comment.rb

Overview

Manage the groups content and handle operations on the groups.

Defined Under Namespace

Classes: Comment, Empty, GroupBeginComment, GroupEndComment, Line, Pattern, UnrecognizedLine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



19
20
21
# File 'lib/codeowners/checker/group.rb', line 19

def initialize
  @list = []
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



13
14
15
# File 'lib/codeowners/checker/group.rb', line 13

def parent
  @parent
end

Class Method Details

.parse(lines) ⇒ Object



15
16
17
# File 'lib/codeowners/checker/group.rb', line 15

def self.parse(lines)
  new.parse(lines)
end

Instance Method Details

#==(other) ⇒ Object



112
113
114
# File 'lib/codeowners/checker/group.rb', line 112

def ==(other)
  other.kind_of?(Group) && other.list == list
end

#add(line) ⇒ Object



90
91
92
93
# File 'lib/codeowners/checker/group.rb', line 90

def add(line)
  line.parent = self
  @list << line
end

#create_subgroupObject



83
84
85
86
87
88
# File 'lib/codeowners/checker/group.rb', line 83

def create_subgroup
  group = self.class.new
  group.parent = self
  @list << group
  group
end

#each(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/codeowners/checker/group.rb', line 23

def each(&block)
  @list.each do |object|
    if object.is_a?(Group)
      object.each(&block)
    else
      block.call(object)
    end
  end
end

#insert(line) ⇒ Object



95
96
97
98
99
# File 'lib/codeowners/checker/group.rb', line 95

def insert(line)
  line.parent = self
  index = insert_at_index(line)
  @list.insert(index, line)
end

#ownerObject



57
58
59
# File 'lib/codeowners/checker/group.rb', line 57

def owner
  owners.first
end

#ownersObject

Owners are ordered by the amount of occurrences



62
63
64
65
66
# File 'lib/codeowners/checker/group.rb', line 62

def owners
  all_owners.group_by(&:itself).sort_by do |_owner, occurrences|
    -occurrences.count
  end.map(&:first)
end

#parse(lines) ⇒ Object



33
34
35
# File 'lib/codeowners/checker/group.rb', line 33

def parse(lines)
  LineGrouper.call(self, lines)
end

#remove(line) ⇒ Object



101
102
103
104
# File 'lib/codeowners/checker/group.rb', line 101

def remove(line)
  @list.safe_delete(line)
  remove! unless any?(Pattern)
end

#remove!Object



106
107
108
109
110
# File 'lib/codeowners/checker/group.rb', line 106

def remove!
  @list.clear
  parent&.remove(self)
  self.parent = nil
end

#subgroups_owned_by(owner) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/codeowners/checker/group.rb', line 68

def subgroups_owned_by(owner)
  @list.flat_map do |item|
    return [] unless item.is_a?(Group)

    a = []
    a << item if item.owner == owner
    a += item.subgroups_owned_by(owner)
    a
  end.compact
end

#titleObject



79
80
81
# File 'lib/codeowners/checker/group.rb', line 79

def title
  @list.first.to_s
end

#to_contentObject



37
38
39
# File 'lib/codeowners/checker/group.rb', line 37

def to_content
  @list.flat_map(&:to_content)
end

#to_tree(indentation = '') ⇒ Object

Returns an array of strings representing the structure of the group. It indent internal subgroups for readability and debugging purposes.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/codeowners/checker/group.rb', line 43

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