Class: File
Class Method Summary collapse
- .append(path, content) ⇒ Object
- .delete?(path) ⇒ Boolean
- .ext(name) ⇒ Object
-
.is_locked?(lock_file) ⇒ Boolean
exit if File.is_locked?(‘tmp/test.lock’).
-
.write_p(file, data) ⇒ Object
write and create dir.
Class Method Details
.append(path, content) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/overload/file.rb', line 11 def append path, content File.open(path, 'a') do |f| f.flock File::LOCK_EX f.puts content end end |
.delete?(path) ⇒ Boolean
23 24 25 26 27 28 29 30 |
# File 'lib/overload/file.rb', line 23 def delete? path if File.exist?(path) File.delete path true else false end end |
.ext(name) ⇒ Object
18 19 20 21 |
# File 'lib/overload/file.rb', line 18 def ext name out = name.to_s.split('.').last.to_s.downcase [3,4].include?(out.length) ? out : nil end |
.is_locked?(lock_file) ⇒ Boolean
exit if File.is_locked?(‘tmp/test.lock’)
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/overload/file.rb', line 33 def is_locked? lock_file lock_fd = File.open(lock_file, File::RDWR|File::CREAT, 0644) Timeout::timeout(0.1) do lock_fd.flock(File::LOCK_EX) return false end rescue Timeout::Error return true end |
.write_p(file, data) ⇒ Object
write and create dir
4 5 6 7 8 9 |
# File 'lib/overload/file.rb', line 4 def write_p file, data path = file.split('/').reverse.drop(1).reverse.join('/') FileUtils.mkdir_p(path) unless File.exist?(path) self.write file, data data end |