Sha256: 3225b3ff2dd7db63f4cf3c97cf2cde9cb96d4c732158b4050b6f1ece07f9f3d3

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module DirGlobIgnore

  class IgnoreFileLists

    DEFAULT_FILE_NAME = '.ignore'.freeze

    attr_writer :ignore_file_name, :base_dir

    def initialize(base_dir = nil)
      self.base_dir = base_dir
    end

    def ignore_file_name
      @ignore_file_name ||= DEFAULT_FILE_NAME
    end

    def base_dir
      @base_dir ||= Dir.pwd
    end

    def load_ignore_files
      @cache = {}
      ignore_files.each do |ignore_file|
        cache[File.expand_path File.dirname ignore_file] = {
            ignore_file: ignore_file,
            patterns: load_ignore_file(ignore_file),
            ignored_files: []
        }
      end
      build_cached_ignore_lists
    end

    def ignore_file?(file)
      cache.values.each do |info|
        return true if info[:ignored_files].include? File.expand_path(file)
      end
      false
    end

    private

    attr_reader :cache

    def build_cached_ignore_lists
      cache.each do |dir, info|
        info[:patterns].each do |pattern|
          info[:ignored_files].concat Dir.glob(File.join(dir, pattern), File::FNM_DOTMATCH)
        end
      end
    end

    def load_ignore_file(ignore_file)
      File.readlines(ignore_file).map(&:chomp).reject do |entry|
        if entry =~ /^\s*#/
          # Ignore commented lines
          true
        elsif entry =~ /^\s*$/
          # Ignore empty lines
          true
        else
          false
        end
      end
    end

    def ignore_files
      file_pattern = File.join base_dir, '**', ignore_file_name
      Dir.glob file_pattern, File::FNM_DOTMATCH
    end


  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dir_glob_ignore-0.3.0 lib/dir_glob_ignore/ignore_file_lists.rb
dir_glob_ignore-0.2.3 lib/dir_glob_ignore/ignore_file_lists.rb