lib/file_scanner/worker.rb in file_scanner-2.3.0 vs lib/file_scanner/worker.rb in file_scanner-3.0.1

- old
+ new

@@ -1,18 +1,26 @@ +require "find" require "logger" require "file_scanner/filters" -require "file_scanner/loader" module FileScanner class Worker - attr_reader :loader, :filters + SLICE = 1000 + ALL = :all? + ANY = :any? - def initialize(loader:, filters: Filters::defaults, all: false, slice: nil, logger: Logger.new(nil)) - @loader = loader + attr_reader :filters + + def initialize(path:, + filters: Filters::defaults, + slice: SLICE, + all: false, + logger: Logger.new(nil)) + @path = File.expand_path(path) @filters = filters - @mode = mode(all) @slice = slice.to_i + @mode = mode(all) @logger = logger end def call return slices unless block_given? @@ -22,27 +30,29 @@ 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? + all ? 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 paths + Find.find(@path) + end + + private def filtered + paths.lazy.select { |file| filter(file) } + end + private def slices - return [filtered] if @slice.zero? filtered.each_slice(@slice) end end end