Sha256: 69a44310c1b3745fdc84b88d03d29d4684b875f9948310fcd03e1fa79e82cdba

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

module DirGlobIgnore

  class IgnoreFileList

    DEFAULT_FILE_NAME = '.ignore'.freeze

    attr_writer :ignore_file_name, :base_dir
    attr_reader :ignore_list

    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] = load_ignore_file ignore_file
      end
      build_ignore_list
    end

    private

    attr_reader :cache

    def build_ignore_list
      ignore_list = []
      cache.each do |dir, patterns|
        patterns.each do |pattern|
          ignore_list.concat Dir.glob(File.join(dir, pattern), File::FNM_DOTMATCH)
        end
      end
      @ignore_list = ignore_list.sort.uniq
    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
    end


  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dir_glob_ignore-0.1.1 lib/dir_glob_ignore/ignore_file_list.rb
dir_glob_ignore-0.1.0 lib/dir_glob_ignore/ignore_file_list.rb