Sha256: b9957d55ddff53ba5d8e47bbac09005403bc57839ebc973d393df8ba56d2f522

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require_relative 'group'
require_relative 'array'

module Codeowners
  class Checker
    # Manage CODEOWNERS file reading and re-writing.
    class CodeOwners
      include Enumerable

      attr_reader :file_manager, :transform_line_procs

      def initialize(file_manager, transformers: nil)
        @file_manager = file_manager
        @transform_line_procs = [
          method(:build_line),
          *transformers
        ]
      end

      def persist!
        file_manager.content = main_group.to_file
      end

      def main_group
        @main_group ||= Group.parse(list)
      end

      def each(&block)
        main_group.each(&block)
      end

      def to_content
        main_group.to_content
      end

      private

      def list
        @list ||= begin
          list = @file_manager.content
          transform_line_procs.each do |transform_line_proc|
            list = list.flat_map { |line| transform_line_proc.call(line) }.compact
          end
          list
        end
      end

      def build_line(line)
        Codeowners::Checker::Group::Line.build(line)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codeowners-checker-1.0.4 lib/codeowners/checker/code_owners.rb