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
41 42 43 44 45 46 |
# File 'lib/rmtools/fs/file.rb', line 41 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 37 38 |
# File 'lib/rmtools/fs/file.rb', line 34 def modify(pattern, bak=true, &block) Dir[pattern].select {|path| file?(path) && __modify(path, 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
88 89 90 91 92 |
# File 'lib/rmtools/fs/file.rb', line 88 def cp(df) dir = File.dirname df FileUtils.mkpath dir unless File.directory? dir FileUtils.cp path, df end |
#ext ⇒ Object
78 79 80 |
# File 'lib/rmtools/fs/file.rb', line 78 def ext name[/[^.]+$/] end |
#include?(str) ⇒ Boolean
63 64 65 66 67 |
# File 'lib/rmtools/fs/file.rb', line 63 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
94 95 96 97 98 |
# File 'lib/rmtools/fs/file.rb', line 94 def mv(df) dir = File.dirname df FileUtils.mkpath dir unless File.directory? dir File.rename path, df end |
#parent ⇒ Object
69 70 71 72 |
# File 'lib/rmtools/fs/file.rb', line 69 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rmtools/fs/file.rb', line 102 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 |