Class: ImageOptim::Path

Inherits:
FSPath
  • Object
show all
Defined in:
lib/image_optim/path.rb

Overview

FSPath with additional helpful methods

Direct Known Subclasses

CachePath, OptimizedPath

Constant Summary collapse

NULL =
if defined?(IO::NULL)
  IO::NULL
else
  %w[/dev/null NUL: NUL nul NIL: NL:].find{ |dev| File.exist?(dev) }
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(path) ⇒ Object

Returns path if it is already an instance of this class otherwise new instance


66
67
68
# File 'lib/image_optim/path.rb', line 66

def self.convert(path)
  path.is_a?(self) ? path : new(path)
end

Instance Method Details

#copy(dst, preserve = false) ⇒ Object

Copy file to dst, optionally preserving attributes

See FileUtils.copy_file


22
23
24
# 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) ⇒ Object

Copy metadata: uid, gid, mode, optionally atime and mtime

Adapted from FileUtils::Entry_#copy_metadata by Minero Aoki


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/image_optim/path.rb', line 37

def (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_formatObject

Get format using ImageSize


60
61
62
# File 'lib/image_optim/path.rb', line 60

def image_format
  ImageMeta.format_for_path(self)
end

#move(dst) ⇒ Object

Move file to dst: rename on same device, copy and unlink original otherwise

See FileUtils.mv


30
31
32
# File 'lib/image_optim/path.rb', line 30

def move(dst)
  FileUtils.move(self, dst)
end

#replace(dst) ⇒ Object

Atomic replace dst with self


50
51
52
53
54
55
56
57
# 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.(temp)
    temp.rename(dst.to_s)
  end
end

#temp_path(*args, &block) ⇒ Object

Get temp path for this file with same extension


14
15
16
17
# 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