Class: Dir

Inherits:
Object show all
Includes:
RMTools::ValueTraversal
Defined in:
lib/rmtools/fs/dir.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RMTools::ValueTraversal

#depth_first_find, #depth_first_map, #depth_first_select, #depth_first_traverse, #preorder_find, #preorder_select, #preorder_traverse

Class Method Details

.threadify(threads = 4, &block) ⇒ Object



97
98
99
# File 'lib/rmtools/fs/dir.rb', line 97

def self.threadify(threads=4, &block)
  RMTools::threadify(Dir['*'], threads, &block)
end

Instance Method Details

#child(idx) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rmtools/fs/dir.rb', line 40

def child(idx)
  df = content[idx]
  if File.file?(df)
    File.new(df)
  elsif File.directory?(df)
    Dir.new(df)
  end
end

#childrenObject



49
50
51
52
53
54
55
56
57
# File 'lib/rmtools/fs/dir.rb', line 49

def children
  content.map {|df| 
    if File.file?(df)
      File.new(df)
    elsif File.directory?(df)
      Dir.new(df)
    end                    
  }
end

#contentObject



30
31
32
33
# File 'lib/rmtools/fs/dir.rb', line 30

def content
  list = Dir["#{path}/**"]
  list.empty? ? to_a[2..-1].sort.map {|c| File.join path, c} : list
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/rmtools/fs/dir.rb', line 13

def include?(name)
  #content.map {|f| File.split(f)[1]}.include? name
  entries.include? name
end

#inspectObject



64
65
66
67
68
69
70
71
# File 'lib/rmtools/fs/dir.rb', line 64

def inspect
  displaypath = case path
        when /^(\/|\w:)/ then path
        when /^\./ then File.join(Dir.pwd, path[1..-1])
        else File.join(Dir.pwd, path)
      end
  "<#Dir \"#{displaypath}\" #{to_a.size - 2} elements>"
end

#nameObject



73
74
75
# File 'lib/rmtools/fs/dir.rb', line 73

def name
  File.basename(path)
end

#parentObject



35
36
37
38
# File 'lib/rmtools/fs/dir.rb', line 35

def parent
  newpath = File.dirname(path)
  Dir.new(newpath) if newpath != path 
end

#real_nameObject

Fixing windoze path problems requires amatch gem for better performance



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rmtools/fs/dir.rb', line 79

def real_name
  n, p, count = name, parent, []
  return n if !p
  pp, pc, sc = parent.path, parent.to_a[2..-1], to_a
  if defined? Amatch
    ms = pc.sizes.max
    count = [:hamming_similar, :levenshtein_similar, :jaro_similar].sum {|m| pc.group_by {|_| _.upcase.ljust(ms).send(m, n)}.max[1]}.count.to_a
    max = count.lasts.max
    res = count.find {|c|
      c[1] == max and File.directory?(df=File.join(pp, c[0])) and Dir.new(df).to_a == sc
    }
    return res[0] if res
  end
  (pc - count).find {|c|
    File.directory?(df=File.join(pp, c)) and Dir.new(df).to_a == sc
  }
end

#recursive_content(flat = true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rmtools/fs/dir.rb', line 18

def recursive_content(flat=true)
  list = []
  cont = content.map {|f|
    if File.directory?(f)
           rc = Dir.new(f).recursive_content(flat)
           flat ? list.concat(rc) : rc
    else flat ? (list << f.sub(/^\.\//, '')) : f.sub(/^\.\//, '')
    end
  }
  (flat ? list : cont)
end

#refreshObject



59
60
61
62
# File 'lib/rmtools/fs/dir.rb', line 59

def refresh
  return if !File.directory?(path)
  Dir.new(path)
end

#to_traversableObject



9
10
11
# File 'lib/rmtools/fs/dir.rb', line 9

def to_traversable
  RMTools::ValueTraversable.new(children)
end