Class: Tins::Find::Finder
Overview
The Finder class implements the core file system traversal logic. It handles path processing, error management, and directory traversal.
Defined Under Namespace
Modules: PathExtension
Instance Attribute Summary collapse
-
#follow_symlinks ⇒ Boolean
Controls whether symbolic links should be followed during traversal.
-
#raise_errors ⇒ Boolean
Controls whether errors during file system operations should be raised.
-
#show_hidden ⇒ Boolean
Controls whether hidden files and directories are included in the search.
-
#suffix ⇒ Array<String>
The file suffix filter, if specified.
Instance Method Summary collapse
-
#find(*paths) {|String| ... } ⇒ Enumerator
Performs a depth-first search of the specified paths.
-
#initialize(opts = {}) ⇒ Finder
constructor
Initializes a new Finder instance with specified options.
-
#prepare_path(path) ⇒ String
Prepares a path for processing by extending it with PathExtension.
-
#protect_from_errors(errors = Find::EXPECTED_STANDARD_ERRORS) { ... } ⇒ Object?
Executes a block while protecting against expected standard errors.
-
#visit_path?(path) ⇒ Boolean
Determines if a path should be visited based on the configured filters.
Constructor Details
#initialize(opts = {}) ⇒ Finder
Initializes a new Finder instance with specified options
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/tins/find.rb', line 116 def initialize(opts = {}) @show_hidden = opts.fetch(:show_hidden) { true } @raise_errors = opts.fetch(:raise_errors) { false } @follow_symlinks = opts.fetch(:follow_symlinks) { true } if opts.key?(:visit) && opts.key?(:suffix) raise ArgumentError, 'either use visit or suffix argument' elsif opts.key?(:visit) @visit = opts.fetch(:visit) { -> path { true } } elsif opts.key?(:suffix) @suffix = Array(opts[:suffix]) @visit = -> path { @suffix.nil? || @suffix.empty? || @suffix.include?(path.suffix) } end end |
Instance Attribute Details
#follow_symlinks ⇒ Boolean
Controls whether symbolic links should be followed during traversal
140 141 142 |
# File 'lib/tins/find.rb', line 140 def follow_symlinks @follow_symlinks end |
#raise_errors ⇒ Boolean
Controls whether errors during file system operations should be raised
136 137 138 |
# File 'lib/tins/find.rb', line 136 def raise_errors @raise_errors end |
#show_hidden ⇒ Boolean
Controls whether hidden files and directories are included in the search
132 133 134 |
# File 'lib/tins/find.rb', line 132 def show_hidden @show_hidden end |
Instance Method Details
#find(*paths) {|String| ... } ⇒ Enumerator
Performs a depth-first search of the specified paths
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/tins/find.rb', line 163 def find(*paths) block_given? or return enum_for(__method__, *paths) paths.collect! { |d| d.dup } while path = paths.shift path = prepare_path(path) catch(:prune) do stat = path.finder_stat or next visit_path?(path) and yield path if stat.directory? ps = protect_from_errors { Dir.entries(path) } or next ps.sort! ps.reverse_each do |p| next if p == "." or p == ".." next if !@show_hidden && p.start_with?('.') p = File.join(path, p) paths.unshift p end end end end end |
#prepare_path(path) ⇒ String
Prepares a path for processing by extending it with PathExtension
189 190 191 192 193 194 |
# File 'lib/tins/find.rb', line 189 def prepare_path(path) path = path.dup path.extend PathExtension path.finder = self path end |
#protect_from_errors(errors = Find::EXPECTED_STANDARD_ERRORS) { ... } ⇒ Object?
Executes a block while protecting against expected standard errors
201 202 203 204 205 206 |
# File 'lib/tins/find.rb', line 201 def protect_from_errors(errors = Find::EXPECTED_STANDARD_ERRORS) yield rescue errors raise_errors and raise return end |
#visit_path?(path) ⇒ Boolean
Determines if a path should be visited based on the configured filters
150 151 152 153 154 155 156 |
# File 'lib/tins/find.rb', line 150 def visit_path?(path) if !defined?(@visit) || @visit.nil? true else @visit.(path) end end |