lib/longleaf/candidates/file_selector.rb in longleaf-1.0.0 vs lib/longleaf/candidates/file_selector.rb in longleaf-1.1.0
- old
+ new
@@ -1,6 +1,7 @@
require 'longleaf/logging'
+require 'longleaf/candidates/physical_path_provider'
module Longleaf
# Selects and allows for iteration over files which match a provided set of selection criteria
class FileSelector
include Longleaf::Logging
@@ -8,11 +9,12 @@
SPECIFICITY_STORAGE_LOCATION = 'storage_location'
attr_reader :specificity
# May only provide either file_paths or storage_locations
- def initialize(file_paths: nil, storage_locations: nil, app_config:)
+ def initialize(file_paths: nil, storage_locations: nil, physical_provider: Longleaf::PhysicalPathProvider.new,
+ app_config:)
if nil_or_empty?(file_paths) && nil_or_empty?(storage_locations)
raise ArgumentError.new("Must provide either file paths or storage locations")
end
if !nil_or_empty?(file_paths) && !nil_or_empty?(storage_locations)
raise ArgumentError.new("Cannot provide both file paths and storage locations")
@@ -34,10 +36,11 @@
path
end
end
# The set of storage locations to select file paths from
@storage_locations = storage_locations
+ @physical_provider = physical_provider
# Validate that the selected storage locations are known
if @storage_locations.nil?
@specificity = SPECIFICITY_PATH
else
@specificity = SPECIFICITY_STORAGE_LOCATION
@@ -61,11 +64,11 @@
end
@target_paths
end
- # Get the next file path for this selector.
+ # Get the next logical file path for this selector.
# @return [String] an absolute path to the next file targeted by this selector,
# or nil if no more files selected
def next_path
if @paths.nil?
# Start the paths listing out from the targetted set of paths for this selector
@@ -78,23 +81,31 @@
# Get the most recently added path for depth first traversal of selected paths
path = @paths.pop
until path.nil? do
@app_config.location_manager.verify_path_in_location(path)
+ physical_path = @physical_provider.get_physical_path(path)
+ separate_logical = physical_path != path
+ if separate_logical
+ @app_config.location_manager.verify_path_in_location(physical_path)
+ end
- if File.exist?(path)
- if File.directory?(path)
+ if File.exist?(physical_path)
+ if File.directory?(physical_path)
+ if separate_logical
+ raise InvalidStoragePathError.new("Cannot specify physical path to a directory: #{physical_path}")
+ end
logger.debug("Expanding directory #{path}")
# For a directory, add all children to file_paths
Dir.entries(path).sort.reverse_each do |child|
@paths << File.join(path, child) unless child == '.' or child == '..'
end
else
logger.debug("Returning file #{path}")
return path
end
else
- raise InvalidStoragePathError.new("File #{path} does not exist.")
+ raise InvalidStoragePathError.new("File #{physical_path} does not exist.")
end
# Returned path was not a suitable file, try the next path
path = @paths.pop
end