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



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

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

Instance Method Details

#child(idx) ⇒ Object



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

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

#childrenObject



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

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

#content(opts = {}) ⇒ Object



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

def content(opts={})
  Dir["#{path}/#{'**/' if opts.recursive}#{'{.,}' if opts.include_dot}*"].
  reject {|p| p =~ %r{/\.\.?$}}.map! {|p| p.sub(/^\.\//, '')}
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(name)
  to_a.include? name
end

#inspectObject



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

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



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

def name
  File.basename(path)
end

#parentObject



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

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



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

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, opts = {}) ⇒ Object



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

def recursive_content(flat=true, opts={})
  if opts[:recursive] = flat
    return content(opts)
  end
  content(opts).map {|p|
    if File.directory?(p)
           Dir.new(p).recursive_content(false)
    else p
    end
  }
end

#refreshObject



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

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