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



120
121
122
# File 'lib/codeowners/checker/group.rb', line 120

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

#add(line) ⇒ Object



98
99
100
101
# File 'lib/codeowners/checker/group.rb', line 98

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

#create_subgroupObject



91
92
93
94
95
96
# File 'lib/codeowners/checker/group.rb', line 91

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
      yield(object)
    end
  end
end

#insert(line) ⇒ Object



103
104
105
106
107
# File 'lib/codeowners/checker/group.rb', line 103

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

#ownerObject

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



65
66
67
# File 'lib/codeowners/checker/group.rb', line 65

def owner
  owners.first
end

#ownersObject

Owners are ordered by the amount of occurrences



70
71
72
73
74
# 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) ⇒ Object



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

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

#remove(line) ⇒ Object



109
110
111
112
# 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!Object



114
115
116
117
118
# File 'lib/codeowners/checker/group.rb', line 114

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

#subgroups_owned_by(owner) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# 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

#titleObject



87
88
89
# File 'lib/codeowners/checker/group.rb', line 87

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_fileObject



41
42
43
# File 'lib/codeowners/checker/group.rb', line 41

def to_file
  @list.flat_map(&:to_file)
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. rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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