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 66
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 22
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 37
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
    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 60
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 30
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 50
def replace(dst)
  dst = self.class.new(dst)
  dst.temp_path(dst.dirname) do |temp|
    move(temp)
    dst.copy_metadata(temp)
    temp.rename(dst.to_s)
  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 14
def temp_path(*args, &block)
  ext = extname
  self.class.temp_file_path([basename(ext).to_s, ext], *args, &block)
end