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



118
119
120
# File 'lib/codeowners/checker/group.rb', line 118

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

#add(line) ⇒ Object



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

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

#create_subgroupObject



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

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



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

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



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

def owner
  owners.first
end

#ownersObject

Owners are ordered by the amount of occurrences



68
69
70
71
72
# File 'lib/codeowners/checker/group.rb', line 68

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



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

def remove(line)
  @list.safe_delete(line)
  remove! unless any? { |object| object.is_a? Pattern }
end

#remove!Object



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

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

#subgroups_owned_by(owner) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/codeowners/checker/group.rb', line 74

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



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

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.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/codeowners/checker/group.rb', line 47

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