Sha256: 863f0d4beb9e2e58bbe5ffec3285349a860fbccd0bd9650d392f3f6cf59b4dcf

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

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

module FileScanner
  class Worker
    attr_reader :loader, :filters

    def initialize(loader:, filters: Filters::defaults, all: false, slice: nil, logger: Logger.new(nil))
      @loader = loader
      @filters = filters
      @mode = mode(all)
      @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 filtered
      @loader.call.select { |file| filter(file) }
    end

    private def mode(all)
      return :all? if all
      :any?
    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 slices
      return [filtered] if @slice.zero?
      filtered.each_slice(@slice)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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