Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/extensions/jruby.rb

Class Method Summary collapse

Class Method Details

.binwrite(path, data) ⇒ Object

何故かエンコーディングエラーが出るため



20
21
22
23
24
# File 'lib/extensions/jruby.rb', line 20

def self.binwrite(path, data)
  open(path, "wb") do |fp|
    fp.write(data)
  end
end

.mtime(path) ⇒ Object

-Dfile.encoding=UTF-8 を指定するとなぜか File.mtime がマルチバイト文字を含むパスを認識出来ないため



27
28
29
30
31
# File 'lib/extensions/jruby.rb', line 27

def self.mtime(path)
  java_path = java.nio.file.FileSystems.default.getPath(path)
  java_file_time = java.nio.file.Files.getLastModifiedTime(java_path)
  Time.parse(java_file_time.to_s).getlocal("+09:00")
end

.write(path, string, *options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/extension.rb', line 21

def File.write(path, string, *options)
  dirpath = File.dirname(path)
  backup = false
  temp_path =
    if File.extname(path) == ".yaml" && File.basename(dirpath) != Downloader::SECTION_SAVE_DIR_NAME
      backup = true
      "#{path}.backup"
    else
      File.join(dirpath, SecureRandom.hex(15))
    end

  super(temp_path, string, *options)

  if backup
    super(path, string, *options)
  else
    File.rename(temp_path, path)
  end
end