Class: File
Constant Summary collapse
- PathMemo =
{}
Class Method Summary collapse
- .include?(name, str) ⇒ Boolean
- .modify(pattern, bak = true, &block) ⇒ Object
- .real_name(df) ⇒ Object
- .real_path(path, memo = 1) ⇒ Object
Instance Method Summary collapse
- #cp(df) ⇒ Object
- #ext ⇒ Object
- #include?(str) ⇒ Boolean
- #inspect ⇒ Object
- #mv(df) ⇒ Object
- #name ⇒ Object
- #parent ⇒ Object
-
#real_name ⇒ Object
Fixing windoze path problems requires amatch gem for better performance.
- #refresh ⇒ Object
Class Method Details
.include?(name, str) ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/rmtools/fs/file.rb', line 39 def include?(name, str) f = new name, 'r' incl = f.include? str f.close incl end |
.modify(pattern, bak = true, &block) ⇒ Object
34 35 36 |
# File 'lib/rmtools/fs/file.rb', line 34 def modify(pattern, bak=true, &block) Dir[pattern].select {|file| __modify(file, bak, &block)} end |
.real_name(df) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/rmtools/fs/file.rb', line 26 def real_name(df) if file?(df) new(df).real_name elsif directory?(df) Dir.new(df).real_name end end |
.real_path(path, memo = 1) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rmtools/fs/file.rb', line 13 def real_path path, memo=1 a = (path).split(/[\/\\]/) a.each_index {|i| if a[j=-(i+1)]['~'] n = i+2>a.size ? a[j] : join(a[0..-(i+2)], a[j]) a[j] = PathMemo[n] || real_name(n) PathMemo[n] = a[j] if memo else break end } a*'/' end |
Instance Method Details
#cp(df) ⇒ Object
86 87 88 89 90 |
# File 'lib/rmtools/fs/file.rb', line 86 def cp(df) dir = File.dirname df FileUtils.mkpath dir unless File.directory? dir FileUtils.cp path, df end |
#ext ⇒ Object
76 77 78 |
# File 'lib/rmtools/fs/file.rb', line 76 def ext name[/[^.]+$/] end |
#include?(str) ⇒ Boolean
61 62 63 64 65 |
# File 'lib/rmtools/fs/file.rb', line 61 def include?(str) while s = gets return true if s.include? str end end |
#inspect ⇒ Object
7 8 9 |
# File 'lib/rmtools/fs/file.rb', line 7 def inspect "<#File \"#{path}\" #{closed? ? 'closed' : stat.size.bytes}>" end |
#mv(df) ⇒ Object
92 93 94 95 96 |
# File 'lib/rmtools/fs/file.rb', line 92 def mv(df) dir = File.dirname df FileUtils.mkpath dir unless File.directory? dir File.rename path, df end |
#parent ⇒ Object
67 68 69 70 |
# File 'lib/rmtools/fs/file.rb', line 67 def parent newpath = File.dirname(path) Dir.new(newpath) if newpath != path end |
#real_name ⇒ Object
Fixing windoze path problems requires amatch gem for better performance
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rmtools/fs/file.rb', line 100 def real_name n, p, count = name, parent, [] pp, pc, ss = parent.path, parent.to_a[2..-1], stat ms = pc.sizes.max n, ext = n.rsplit('.', 2) if ext re = /\.#{ext}$/i pc.reject! {|f| !f[re]} end if defined? Amatch count = [:hamming_similar, :levenshtein_similar, :jaro_similar].sum {|m| pc.group_by {|f| (ext ? f[0..-(ext.size+2)] : f).upcase.ljust(ms).send(m, n)}.max[1]}.arrange.to_a max = count.lasts.max res = count.find {|c| c[1] == max and File.file?(df=File.join(pp, c[0])) and File.stat(df) == ss } return res[0] if res end (pc - count).find {|c| File.file?(df=File.join(pp, c)) and File.stat(df) == ss } end |