Sha256: f75bfa24f1d005ad0bfeaebb7ad2cbcb67ccb6116c4ad0ea2e506827a13f4cb0

Contents?: true

Size: 940 Bytes

Versions: 5

Compression:

Stored size: 940 Bytes

Contents

# typed: true
# frozen_string_literal: true

require "singleton"

module Packwerk
  module Parsers
    class Factory
      extend T::Sig
      include Singleton

      RUBY_REGEX = %r{
        # Although not important for regex, these are ordered from most likely to match to least likely.
        \.(rb|rake|builder|gemspec|ru)\Z
        |
        (Gemfile|Rakefile)\Z
      }x
      private_constant :RUBY_REGEX

      ERB_REGEX = /\.erb\Z/
      private_constant :ERB_REGEX

      sig { params(path: String).returns(T.nilable(ParserInterface)) }
      def for_path(path)
        case path
        when RUBY_REGEX
          @ruby_parser ||= Ruby.new
        when ERB_REGEX
          @erb_parser ||= erb_parser_class.new
        end
      end

      def erb_parser_class
        @erb_parser_class ||= Erb
      end

      def erb_parser_class=(klass)
        @erb_parser_class = klass
        @erb_parser = nil
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
packwerk-2.3.0 lib/packwerk/parsers/factory.rb
packwerk-2.2.2 lib/packwerk/parsers/factory.rb
packwerk-2.2.1 lib/packwerk/parsers/factory.rb
packwerk-2.2.0 lib/packwerk/parsers/factory.rb
packwerk-2.1.1 lib/packwerk/parsers/factory.rb