Sha256: fd75595051aacb1c2fdda1dfc643d37634fcdace704308e89ac7da25e194198b

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

class FastIgnore
  class Rule
    using DeletePrefixSuffix unless RUBY_VERSION >= '2.5'

    def initialize(rule, root: Dir.pwd)
      @root = root
      @rule = rule
      strip!
      return if skip?

      @rule = @rule.delete_prefix('!') if negation?
      @rule = @rule.delete_suffix('/') if dir_only?
      @rule = "#{prefix}#{@rule}"
    end

    def negation?
      @negation ||= @rule.start_with?('!')
    end

    def dir_only?
      @dir_only ||= @rule.end_with?('/')
    end

    def match?(path, dir: File.directory?(path))
      return false if !dir && dir_only?

      path = path.delete_prefix(root)

      File.fnmatch?(@rule, path, File::FNM_DOTMATCH | File::FNM_PATHNAME)
    end

    def empty?
      @empty ||= @rule.empty?
    end

    def comment?
      @comment ||= @rule.start_with?('#')
    end

    def skip?
      empty? || comment?
    end

    private

    attr_reader :root

    def prefix
      @prefix ||= if @rule.start_with?('/')
        ''
      elsif @rule.end_with?('/**') || @rule.include?('/**/')
        '/'
      else
        '**/'
      end
    end

    def strip!
      @rule = @rule.chomp
      @rule = @rule.rstrip unless @rule.end_with?('\\ ')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fast_ignore-0.3.2 lib/fast_ignore/rule.rb
fast_ignore-0.3.1 lib/fast_ignore/rule.rb
fast_ignore-0.3.0 lib/fast_ignore/rule.rb