Sha256: dba9b57b6514d3dc57c7113baade27d9481427f17f789e7021ad5a73f86a8f8f

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

# typed: strict
# 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 { void }
      def initialize
        @ruby_parser = T.let(nil, T.nilable(ParserInterface))
        @erb_parser = T.let(nil, T.nilable(ParserInterface))
        @erb_parser_class = T.let(nil, T.nilable(Class))
      end

      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 ||= T.unsafe(erb_parser_class).new
        end
      end

      sig { returns(Class) }
      def erb_parser_class
        @erb_parser_class ||= Erb
      end

      sig { params(klass: T.nilable(Class)).void }
      def erb_parser_class=(klass)
        @erb_parser_class = klass
        @erb_parser = nil
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
packwerk-3.2.0 lib/packwerk/parsers/factory.rb
packwerk-3.1.0 lib/packwerk/parsers/factory.rb
packwerk-3.0.1 lib/packwerk/parsers/factory.rb
packwerk-3.0.0 lib/packwerk/parsers/factory.rb