Class: File

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

Class Method Summary collapse

Class Method Details

.binwrite(path, data) ⇒ Object

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



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

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

.mtime(path) ⇒ Object

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



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

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, mode: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/extension.rb', line 29

def File.write(path, string, *options, mode: nil)
  return super if mode

  dirpath = File.dirname(path)
  FileUtils.makedirs(dirpath) unless Dir.exist?(dirpath)
  temp_path = File.join(dirpath, SecureRandom.hex(15))
  if File.extname(path) == ".yaml" && File.basename(dirpath) != Downloader::SECTION_SAVE_DIR_NAME
    backup = "#{path}.backup"
  end

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