class DirFriend::D

Attributes

entries[R]

Public Class Methods

new(name='.', level:0, depth:Float::MAX.to_i, exclude:[]) click to toggle source
Calls superclass method
# File lib/dir_friend/d.rb, line 13
def initialize(name='.', level:0, depth:Float::MAX.to_i, exclude:[])
  super(name, level:level)
  @entries = []
  @exclude = exclude.map { |ex| File.expand_path ex }
  build(depth) if depth >= 1
  self
end

Public Instance Methods

depth() click to toggle source
# File lib/dir_friend/d.rb, line 46
def depth
  @depth ||= map(&:level).max
end
down(child=nil) click to toggle source
# File lib/dir_friend/d.rb, line 37
def down(child=nil)
  unless child
    min = entries.select(&:directory?).min
    return min unless min
    child = min.name
  end
  D.new File.join(path, child)
end
each(&blk) click to toggle source
# File lib/dir_friend/d.rb, line 21
def each(&blk)
  entries.each do |e|
    blk.call(e)
    e.each(&blk) if e.is_a?(D)
  end
end
info() click to toggle source
# File lib/dir_friend/d.rb, line 28
def info
  dirs, files = group_by { |f| f.is_a? D }.map { |_, fs| fs.size }
  {directories: dirs, files: files, depth: depth}
end
to_dot(opt={}) click to toggle source
# File lib/dir_friend/d.rb, line 54
def to_dot(opt={})
  graph = DirFriend::Graph.new(self)
  if opt.delete(:open) && OS.mac?
    Tempfile.open(['dirfriend', '.dot']) do |f|
      f.puts graph.build(opt)
      if system("open", f.path)
        puts "Graphviz opened tempfile: #{f.path}"
      end
    end
  else
    graph.build(opt)
  end
rescue
  abort "something go wrong."
end
to_s() click to toggle source
# File lib/dir_friend/d.rb, line 50
def to_s
  "D: #{name}"
end
up() click to toggle source
# File lib/dir_friend/d.rb, line 33
def up
  D.new path.sub(/\/[^\/]+$/, '')
end

Private Instance Methods

build(depth) click to toggle source
# File lib/dir_friend/d.rb, line 71
def build(depth)
  entries = Dir[File.join(path, '*')]
  entries.each do |ent|
    next if @exclude.include?(File.expand_path ent)
    @entries << Any.new(ent, level:level+1, depth:depth-1, exclude:@exclude)
  end
end