class Tins::Find::Finder
Attributes
Public Class Methods
Source
# File lib/tins/find.rb, line 57 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
Public Instance Methods
Source
# File lib/tins/find.rb, line 87 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
Source
# File lib/tins/find.rb, line 109 def prepare_path(path) path = path.dup path.extend PathExtension path.finder = self path end
Source
# File lib/tins/find.rb, line 116 def protect_from_errors(errors = Find::EXPECTED_STANDARD_ERRORS) yield rescue errors raise_errors and raise return end
Source
# File lib/tins/find.rb, line 79 def visit_path?(path) if !defined?(@visit) || @visit.nil? true else @visit.(path) end end