Module: DTC::Utils::Visitor::Folder

Defined in:
lib/dtc/utils/visitor/folder.rb

Defined Under Namespace

Classes: FilenameFilteringVisitor

Class Method Summary collapse

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, options = {}
  visitor = visitor.new() if visitor.is_a?(Class)
  options = options.is_a?(Fixnum) ? ({:max_depth => options}) : options.dup
  max_depth = options.delete(:max_depth) { -1 }
  filter = options.empty? ? visitor :
    FilenameFilteringVisitor.new(visitor, options)
  accept_path filter, File.expand_path(path), max_depth, (options[:sorted].nil? ? true : options[: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