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)


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 = 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



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

#extObject



76
77
78
# File 'lib/rmtools/fs/file.rb', line 76

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

#include?(str) ⇒ Boolean

Returns:

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

#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



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

#nameObject



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

def name
  File.basename(path)
end

#parentObject



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_nameObject

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

#refreshObject



80
81
82
83
84
# File 'lib/rmtools/fs/file.rb', line 80

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