class ImageOptim::Path
FSPath with additional helpful methods
Constants
- NULL
Public Class Methods
convert(path)
click to toggle source
Returns path if it is already an instance of this class otherwise new instance
# File lib/image_optim/path.rb, line 73 def self.convert(path) path.is_a?(self) ? path : new(path) end
Public Instance Methods
copy(dst, preserve = false)
click to toggle source
Copy file to dst, optionally preserving attributes
See FileUtils.copy_file
# File lib/image_optim/path.rb, line 24 def copy(dst, preserve = false) FileUtils.copy_file(self, dst, preserve) end
copy_metadata(dst, time = false)
click to toggle source
Copy metadata: uid, gid, mode, optionally atime and mtime
Adapted from FileUtils::Entry_#copy_metadata by Minero Aoki
# File lib/image_optim/path.rb, line 39 def copy_metadata(dst, time = false) stat = lstat dst.utime(stat.atime, stat.mtime) if time begin dst.chown(stat.uid, stat.gid) rescue Errno::EPERM, Errno::EACCES dst.chmod(stat.mode & 0o1777) else dst.chmod(stat.mode) end end
image_format()
click to toggle source
Get format using ImageSize
# File lib/image_optim/path.rb, line 67 def image_format ImageMeta.format_for_path(self) end
move(dst)
click to toggle source
Move file to dst: rename on same device, copy and unlink original otherwise
See FileUtils.mv
# File lib/image_optim/path.rb, line 32 def move(dst) FileUtils.move(self, dst) end
replace(dst)
click to toggle source
Atomic replace dst with self
# File lib/image_optim/path.rb, line 52 def replace(dst) dst = self.class.convert(dst) if same_dev?(dst.dirname) dst.copy_metadata(self) begin rename(dst.to_s) rescue Errno::EXDEV replace_using_tmp_file(dst) end else replace_using_tmp_file(dst) end end
temp_path(*args, &block)
click to toggle source
Get temp path for this file with same extension
# File lib/image_optim/path.rb, line 16 def temp_path(*args, &block) ext = extname self.class.temp_file_path([basename(ext).to_s, ext], *args, &block) end
Protected Instance Methods
replace_using_tmp_file(dst)
click to toggle source
# File lib/image_optim/path.rb, line 83 def replace_using_tmp_file(dst) dst.temp_path_with_tmp_ext(dst.dirname) do |temp| move(temp) dst.copy_metadata(temp) temp.rename(dst.to_s) end end
same_dev?(other)
click to toggle source
# File lib/image_optim/path.rb, line 79 def same_dev?(other) stat.dev == other.stat.dev end
temp_path_with_tmp_ext(*args, &block)
click to toggle source
# File lib/image_optim/path.rb, line 91 def temp_path_with_tmp_ext(*args, &block) self.class.temp_file_path([basename.to_s, '.tmp'], *args, &block) end