Sha256: b4182553298c78b68608dae13d59a79a555437b072088eacdfdf367b9d293bb3

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require "find"
require "logger"
require "file_scanner/filters"

module FileScanner
  class Worker
    ALL = :all?
    ANY = :any?

    attr_reader :path, :filters

    def initialize(path:, 
                   filters: Filters::defaults, 
                   all: false, 
                   filecheck: false,
                   logger: Logger.new(nil))
      @path = File.expand_path(path) << File::SEPARATOR
      @filters = filters
      @mode = mode(all)
      @filecheck = filecheck
      @logger = logger
    end

    def call
      @logger.info { "scanning \e[1m#{@path}\e[0m..." }
      @logger.debug { "skipping directories" } if @filecheck
      @logger.debug { "applying \e[36m#{@filters.size}\e[0m filters by \e[35m#{@mode}\e[0m" }
      paths.lazy.select { |file| valid?(file) && filter(file) }
    rescue StandardError => e
      @logger.error { e.message }
      raise e
    end

    private def mode(all)
      all ? ALL : ANY
    end

    private def valid?(file)
      return true unless @filecheck
      FileTest.file?(file)
    end

    private def filter(file)
      @filters.send(@mode) do |filter|
        @logger.debug { "filtering \e[37m#{File.basename(file)}\e[0m" }
        filter.call(file)
      end
    end

    private def paths
      Find.find(@path)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
file_scanner-3.2.5 lib/file_scanner/worker.rb