Sha256: 019d4668a61127296599014d41f54403a73c57580b7ff6e3e5cc5e9cbeb4c3c7

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require_relative 'line'
require_relative '../owner'
require 'pathspec'

module Codeowners
  class Checker
    class Group
      # Defines and manages line type pattern.
      # Parse the line into pattern, owners and whitespaces.
      class Pattern < Line
        attr_accessor :owners, :whitespace
        attr_reader :pattern, :spec

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

        def initialize(line)
          super
          parse(line)
        end

        def owner
          owners.first
        end

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

        # Parse the line counting whitespaces between pattern and owners.
        def parse(line)
          @pattern, *@owners = line.split(/\s+/)
          @whitespace = line.split('@').first.count(' ') - 1
          @spec = parse_spec(@pattern)
        end

        def match_file?(file)
          spec.match file
        end

        def pattern=(new_pattern)
          @whitespace += @pattern.size - new_pattern.size
          @whitespace = 1 if @whitespace < 1

          @spec = parse_spec(new_pattern)
          @pattern = new_pattern
        end

        # @return String with the pattern and owners
        # Use @param preserve_whitespaces to keep the previous identation.
        def to_file(preserve_whitespaces: true)
          line = pattern
          spaces = preserve_whitespaces ? whitespace : 0
          line << ' ' * spaces
          [line, *owners].join(' ')
        end

        def to_s
          to_file(preserve_whitespaces: false)
        end

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
codeowner_validator-0.4.0 lib/codeowners/checker/group/pattern.rb
codeowners-checker-1.1.2 lib/codeowners/checker/group/pattern.rb
codeowners-checker-1.1.1 lib/codeowners/checker/group/pattern.rb