Sha256: 3553d390e88382f5dedacb62b74fe77bbb801637fbb92642f53748e5ad6aeb5c

Contents?: true

Size: 1.35 KB

Versions: 11

Compression:

Stored size: 1.35 KB

Contents

module Steep
  class Project
    class Pattern
      attr_reader :patterns
      attr_reader :ignores
      attr_reader :prefixes
      attr_reader :ignore_prefixes
      attr_reader :ext

      def initialize(patterns:, ignores: [], ext:)
        @patterns = patterns
        @ignores = ignores
        @ext = ext

        @prefixes = patterns.map do |pat|
          if pat == "." || pat == "./"
            ""
          else
            pat.delete_prefix("./").delete_suffix(File::Separator) << File::Separator
          end
        end
        @ignore_prefixes = ignores.map do |pat|
          if pat == "." || pat == "./"
            ""
          else
            pat.delete_prefix("./").delete_suffix(File::Separator) << File::Separator
          end
        end
      end

      def =~(path)
        unless path.is_a?(Pathname)
          path = Pathname(path.to_s)
        end

        match?(path) && !ignore?(path)
      end

      def match?(path)
        test_string(path, patterns, prefixes)
      end

      def ignore?(path)
        test_string(path, ignores, ignore_prefixes)
      end

      def test_string(path, patterns, prefixes)
        string = path.to_s

        patterns.any? {|pat| File.fnmatch(pat, string, File::FNM_PATHNAME) } ||
          prefixes.any? {|prefix| File.fnmatch("#{prefix}**/*#{ext}", string, File::FNM_PATHNAME) }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
steep-1.9.0.dev.1 lib/steep/project/pattern.rb
steep-1.8.3 lib/steep/project/pattern.rb
steep-1.8.2 lib/steep/project/pattern.rb
steep-1.8.1 lib/steep/project/pattern.rb
steep-1.8.0 lib/steep/project/pattern.rb
steep-1.8.0.pre.2 lib/steep/project/pattern.rb
steep-1.8.0.pre.1 lib/steep/project/pattern.rb
steep-1.8.0.dev.2 lib/steep/project/pattern.rb
steep-1.8.0.dev.1 lib/steep/project/pattern.rb
steep-1.7.1 lib/steep/project/pattern.rb
steep-1.7.0 lib/steep/project/pattern.rb