Module: DTC::Utils::Visitor::Folder
- Defined in:
- lib/dtc/utils/visitor/folder.rb
Defined Under Namespace
Classes: FilenameFilteringVisitor
Class Method Summary collapse
-
.accept(visitor, path, options = {}) ⇒ Object
Visit the path provided using ‘visitor`, (or a transparent FilenameFilteringVisitor if options include filtering).
- .accept_path(visitor, path, max_depth = -1,, sorted) ⇒ Object
Class Method Details
.accept(visitor, path, options = {}) ⇒ Object
Visit the path provided using ‘visitor`, (or a transparent FilenameFilteringVisitor if options include filtering)
‘options` may include a `:max_depth` key, or any keys used by `FilenameFilteringVisitor`
56 57 58 59 60 61 62 63 64 |
# File 'lib/dtc/utils/visitor/folder.rb', line 56 def self.accept visitor, path, = {} visitor = visitor.new() if visitor.is_a?(Class) = .is_a?(Fixnum) ? ({:max_depth => }) : .dup max_depth = .delete(:max_depth) { -1 } filter = .empty? ? visitor : FilenameFilteringVisitor.new(visitor, ) accept_path filter, File.(path), max_depth, ([:sorted].nil? ? true : [:sorted]) visitor end |
.accept_path(visitor, path, max_depth = -1,, sorted) ⇒ Object
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 |
# File 'lib/dtc/utils/visitor/folder.rb', line 78 def self.accept_path visitor, path, max_depth = -1, sorted return unless visitor.enter path dir = Dir.new(path) if sorted dir = dir.each.to_a.sort do |a, b| a_is_dir = File.directory?(File.join(path, a)) b_is_dir = File.directory?(File.join(path, b)) a_is_dir != b_is_dir ? (a_is_dir ? -1 : 1) : self.string_compare(a, b) end end dir.each do |f| full_path = File.join(path, f) next if f == "." || f == ".." if File.directory? full_path return unless File.readable?(path) if max_depth == 0 visitor.leave if visitor.enter(full_path) else self.accept_path(visitor, full_path, max_depth - 1, sorted) end else visitor.add f, full_path end end visitor.leave end |