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



73
74
75
# File 'lib/image_optim/path.rb', line 73

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



24
25
26
# 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) ⇒ Object

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

Adapted from FileUtils::Entry_#copy_metadata by Minero Aoki



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

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

Get format using ImageSize



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

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



32
33
34
# File 'lib/image_optim/path.rb', line 32

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

#replace(dst) ⇒ Object

Atomic replace dst with self



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/image_optim/path.rb', line 52

def replace(dst)
  dst = self.class.convert(dst)
  if same_dev?(dst.dirname)
    dst.(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) ⇒ Object

Get temp path for this file with same extension



16
17
18
19
# 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