Sha256: 472629b88d01c19532bf309804a7fb14f05d772d13f2ee56263fe7ad46afdb2e

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

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

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

    attr_reader :filters

    def initialize(path:, 
                   filters: Filters::defaults, 
                   slice: SLICE, 
                   all: false, 
                   check: false,
                   logger: Logger.new(nil))
      @path = File.expand_path(path)
      @filters = filters
      @slice = slice.to_i
      @mode = mode(all)
      @check = check
      @logger = logger
    end

    def call
      return slices unless block_given?
      slices.each do |slice|
        yield(slice, @logger)
      end
    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 @check
      FileTest.file?(file)
    end

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

    private def paths
      Find.find(@path)
    end

    private def filtered
      paths.lazy.select { |file| valid?(file) && filter(file) }
    end

    private def slices
      filtered.each_slice(@slice)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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