class Codeowners::Checker::Group::Pattern

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

Attributes

owners[RW]
pattern[R]
spec[R]
whitespace[RW]

Public Class Methods

match?(line) click to toggle source
# File lib/codeowners/checker/group/pattern.rb, line 16
def self.match?(line)
  _pattern, *owners = line.split(/\s+/)
  Owner.valid?(*owners)
end
new(line) click to toggle source
Calls superclass method Codeowners::Checker::Group::Line::new
# File lib/codeowners/checker/group/pattern.rb, line 21
def initialize(line)
  super
  parse(line)
end

Public Instance Methods

match_file?(file) click to toggle source
# File lib/codeowners/checker/group/pattern.rb, line 42
def match_file?(file)
  spec.match file
end
owner() click to toggle source
# File lib/codeowners/checker/group/pattern.rb, line 26
def owner
  owners.first
end
parse(line) click to toggle source

Parse the line counting whitespaces between pattern and owners.

# 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) click to toggle source
# File lib/codeowners/checker/group/pattern.rb, line 67
def parse_spec(pattern)
  PathSpec.from_lines(pattern)
end
pattern=(new_pattern) click to toggle source
# File lib/codeowners/checker/group/pattern.rb, line 46
def pattern=(new_pattern)
  @whitespace += @pattern.size - new_pattern.size
  @whitespace = 1 if @whitespace < 1

  @spec = parse_spec(new_pattern)
  @pattern = new_pattern
end
rename_owner(owner, new_owner) click to toggle source
# 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) click to toggle source

@return String with the pattern and owners Use @param preserve_whitespaces to keep the previous identation.

# 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_s() click to toggle source
# File lib/codeowners/checker/group/pattern.rb, line 63
def to_s
  to_file(preserve_whitespaces: false)
end