lib/file_scanner/worker.rb in file_scanner-1.1.0 vs lib/file_scanner/worker.rb in file_scanner-2.0.1

- old
+ new

@@ -1,45 +1,41 @@ require "logger" module FileScanner class Worker - attr_reader :filters, :policies + 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, policies: [], - logger: self.class.default_logger, slice: nil) + def initialize(loader:, filters: Filters::defaults, logger: self.class.default_logger, slice: nil) @loader = loader @filters = filters - @policies = policies @slice = slice.to_i @logger = logger end def call slices.each do |slice| - yield(slice) if block_given? && policies.empty? - policies.each do |policy| - @logger.info { "applying \e[33m#{policy}\e[0m to #{slice.size} files" } - policy.call(slice) - end + yield(slice, @logger) if block_given? end rescue StandardError => e @logger.error { e.message } raise e end private def files - Array(@loader.call).select do |f| - @filters.any? do |filter| - @logger.info { "applying \e[35m#{filter}\e[0m to #{File.basename(f)}" } - filter.call(f) - end + 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?