Class: Codeowners::Checker::Group::Pattern

Inherits:
Line
  • Object
show all
Defined in:
lib/codeowners/checker/group/pattern.rb

Overview

Defines and manages line type pattern. Parse the line into pattern, owners and whitespaces.

Instance Attribute Summary collapse

Attributes inherited from Line

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Line

#<=>, #==, build, #pattern?, #remove!, subclasses, #to_content, #to_tree

Constructor Details

#initialize(line) ⇒ Pattern

Returns a new instance of Pattern.



21
22
23
24
# File 'lib/codeowners/checker/group/pattern.rb', line 21

def initialize(line)
  super
  parse(line)
end

Instance Attribute Details

#ownersObject

Returns the value of attribute owners.



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

def owners
  @owners
end

#patternObject

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

#specObject (readonly)

Returns the value of attribute spec.



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

def spec
  @spec
end

#whitespaceObject

Returns the value of attribute whitespace.



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

def whitespace
  @whitespace
end

Class Method Details

.match?(line) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/codeowners/checker/group/pattern.rb', line 16

def self.match?(line)
  _pattern, *owners = line.split(/\s+/)
  Owner.valid?(*owners)
end

Instance Method Details

#match_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

def match_file?(file)
  spec.match file
end

#ownerObject



26
27
28
# File 'lib/codeowners/checker/group/pattern.rb', line 26

def owner
  owners.first
end

#parse(line) ⇒ Object

Parse the line counting whitespaces between pattern and owners.



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

def parse(line)
  @pattern, *@owners = line.split(/\s+/)
  @whitespace = line.split('@').first.count(' ') - 1
  @spec = parse_spec(@pattern)
end

#parse_spec(pattern) ⇒ Object



67
68
69
# File 'lib/codeowners/checker/group/pattern.rb', line 67

def parse_spec(pattern)
  PathSpec.from_lines(pattern)
end

#rename_owner(owner, new_owner) ⇒ Object



30
31
32
33
# File 'lib/codeowners/checker/group/pattern.rb', line 30

def rename_owner(owner, new_owner)
  owners.delete(owner)
  owners << new_owner unless owners.include?(new_owner)
end

#to_file(preserve_whitespaces: true) ⇒ Object

Use @param preserve_whitespaces to keep the previous identation.

Returns:

  • String with the pattern and owners



56
57
58
59
60
61
# File 'lib/codeowners/checker/group/pattern.rb', line 56

def to_file(preserve_whitespaces: true)
  line = pattern
  spaces = preserve_whitespaces ? whitespace : 0
  line << ' ' * spaces
  [line, *owners].join(' ')
end

#to_sObject



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

def to_s
  to_file(preserve_whitespaces: false)
end