Sha256: 53b9b2600bd5632371e0bbf682b819031ddcd4b353f9dbd0aa7c55821a9bf429

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Git
  module Lint
    module Parsers
      module Trailers
        class Collaborator
          DEFAULT_KEY_PATTERN = /\ACo.*Authored.*By.*\Z/i

          DEFAULT_MATCH_PATTERN = /
            (?<key>\A.+)         # Key (anchored to start of line).
            (?<delimiter>:)      # Key delimiter.
            (?<key_space>\s?)    # Space delimiter (optional).
            (?<name>.*?)         # Collaborator name (smallest possible).
            (?<name_space>\s?)   # Space delimiter (optional).
            (?<email><.+>)?      # Collaborator email (optional).
            \Z                   # End of line.
          /x

          def initialize text,
                         key_pattern: DEFAULT_KEY_PATTERN,
                         match_pattern: DEFAULT_MATCH_PATTERN

            @text = String text
            @key_pattern = key_pattern
            @match_pattern = match_pattern
            @matches = build_matches
          end

          def key
            String matches["key"]
          end

          def name
            String matches["name"]
          end

          def email
            String(matches["email"]).delete_prefix("<").delete_suffix(">")
          end

          def match?
            text.match? key_pattern
          end

          private

          attr_reader :text, :key_pattern, :match_pattern, :matches

          def build_matches
            text.match(match_pattern).then { |data| data ? data.named_captures : Hash.new }
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
git-lint-2.2.1 lib/git/lint/parsers/trailers/collaborator.rb
git-lint-2.2.0 lib/git/lint/parsers/trailers/collaborator.rb
git-lint-2.1.0 lib/git/lint/parsers/trailers/collaborator.rb
git-lint-2.0.0 lib/git/lint/parsers/trailers/collaborator.rb