Class: Longleaf::FileSelector
- Inherits:
-
Object
- Object
- Longleaf::FileSelector
- Includes:
- Logging
- Defined in:
- lib/longleaf/candidates/file_selector.rb
Overview
Selects and allows for iteration over files which match a provided set of selection criteria
Direct Known Subclasses
Constant Summary collapse
- SPECIFICITY_PATH =
'path'
- SPECIFICITY_STORAGE_LOCATION =
'storage_location'
Instance Attribute Summary collapse
-
#specificity ⇒ Object
readonly
Returns the value of attribute specificity.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate through the file paths for this selector and execute the provided block with each.
-
#initialize(file_paths: nil, storage_locations: nil, physical_provider: Longleaf::PhysicalPathProvider.new, app_config:) ⇒ FileSelector
constructor
May only provide either file_paths or storage_locations.
-
#next_path ⇒ String
Get the next logical file path for this selector.
-
#storage_locations ⇒ Object
return [Array] a list of all storage locations being targeted by this selector.
-
#target_paths ⇒ Array
A list of top level paths from which files will be selected.
Methods included from Logging
#initialize_logger, initialize_logger, logger, #logger
Constructor Details
#initialize(file_paths: nil, storage_locations: nil, physical_provider: Longleaf::PhysicalPathProvider.new, app_config:) ⇒ FileSelector
May only provide either file_paths or storage_locations
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/longleaf/candidates/file_selector.rb', line 14 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") end @app_config = app_config # The top level paths targeted by this selector @target_paths = file_paths&.map do |path| # Resolve relative paths against pwd pathname = Pathname.new(path) if !pathname.absolute? path = File.join(Dir.pwd, path) end path = File.(path) # adding trailing /'s to directories if Dir.exist?(path) && !path.end_with?('/') path + '/' else 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 locations = @app_config.location_manager.locations @storage_locations.each do |loc_name| unless locations.key?(loc_name) raise StorageLocationUnavailableError.new("Cannot select unknown storage location #{loc_name}.") end end end end |
Instance Attribute Details
#specificity ⇒ Object (readonly)
Returns the value of attribute specificity.
11 12 13 |
# File 'lib/longleaf/candidates/file_selector.rb', line 11 def specificity @specificity end |
Instance Method Details
#each ⇒ Object
Iterate through the file paths for this selector and execute the provided block with each. A block is required.
117 118 119 120 121 122 123 124 |
# File 'lib/longleaf/candidates/file_selector.rb', line 117 def each file_path = next_path until file_path.nil? yield file_path file_path = next_path end end |
#next_path ⇒ String
Get the next logical file path for this selector. or nil if no more files selected
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/longleaf/candidates/file_selector.rb', line 72 def next_path if @paths.nil? # Start the paths listing out from the targetted set of paths for this selector # In reverse order since using a LIFO structure @paths = target_paths.reverse end # No more paths to return return nil if @paths&.empty? # 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?(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 #{physical_path} does not exist.") end # Returned path was not a suitable file, try the next path path = @paths.pop end end |
#storage_locations ⇒ Object
return [Array] a list of all storage locations being targeted by this selector
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/longleaf/candidates/file_selector.rb', line 127 def storage_locations # Determine what storage_locations are represented by the given file paths if @storage_locations.nil? && !@target_paths.nil? loc_set = Set.new @target_paths.each do |path| loc = @app_config.location_manager.get_location_by_path(path) loc_set.add(loc.name) unless loc.nil? end @storage_locations = loc_set.to_a end if @storage_locations.nil? @storage_locations = Array.new end @storage_locations end |
#target_paths ⇒ Array
Returns a list of top level paths from which files will be selected.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/longleaf/candidates/file_selector.rb', line 57 def target_paths # If starting from locations, initialize by expanding locations out to their actual paths if @target_paths.nil? && !@storage_locations.nil? @target_paths = Array.new @storage_locations.each do |loc_name| @target_paths << @app_config.location_manager.locations[loc_name].path end end @target_paths end |