Sha256: 6a531bf4010eba0b2a0dfba573ca0b15741846b8d4670b7b5266a282ff59ae43

Contents?: true

Size: 1003 Bytes

Versions: 1

Compression:

Stored size: 1003 Bytes

Contents

require "logger"

module FileScanner
  class Worker
    attr_reader :filters

    def self.default_logger
      Logger.new(nil).tap do |logger|
        logger.level = Logger::ERROR
      end
    end

    def initialize(loader:, filters: Filters::defaults, logger: self.class.default_logger, slice: nil)
      @loader = loader
      @filters = filters
      @slice = slice.to_i
      @logger = logger
    end

    def call
      slices.each do |slice|
        yield(slice, @logger) if block_given?
      end
    rescue StandardError => e
      @logger.error { e.message }
      raise e
    end

    private def files
      paths = @loader.call
      paths.select! { |file| filter(file) } || paths
    end

    private def filter(file)
      @filters.any? do |filter|
        @logger.info { "applying \e[33m#{filter}\e[0m to #{File.basename(file)}" }
        filter.call(file)
      end
    end

    private def slices
      return [files] if @slice.zero?
      files.each_slice(@slice)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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