Sha256: 4f3b8893420f6f91003f71214234c7a1fca415961c3b6beaa70c99e90911dd16

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require "logger"
require "file_scanner/filters"
require "file_scanner/loader"

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

    def self.factory(path:, extensions: [], filters: [], logger: default_logger, slice: nil)
      loader = Loader.new(path: path, extensions: extensions)
      new(loader: loader, filters: filters, logger: logger, slice: slice)
    end

    attr_reader :loader, :filters

    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
      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 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.3 lib/file_scanner/worker.rb