Class: File

Inherits:
Object show all
Defined in:
lib/rmtools/fs/file.rb

Constant Summary collapse

PathMemo =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.include?(name, str) ⇒ Boolean

Returns:

  • (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 = expand_path(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

#extObject



78
79
80
# File 'lib/rmtools/fs/file.rb', line 78

def ext
  name[/[^.]+$/]
end

#include?(str) ⇒ Boolean

Returns:

  • (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

#inspectObject



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

#nameObject



74
75
76
# File 'lib/rmtools/fs/file.rb', line 74

def name
  File.basename(path)
end

#parentObject



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_nameObject

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

#refreshObject



82
83
84
85
86
# File 'lib/rmtools/fs/file.rb', line 82

def refresh
  return if !File.file?(path)
  close
  File.new(path,'r')
end