Class: ImageOptim::ImagePath

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

Overview

FSPath with additional helpful methods

Defined Under Namespace

Classes: Optimized

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



64
65
66
# File 'lib/image_optim/image_path.rb', line 64

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

Instance Method Details

#binreadObject

Read binary data



58
59
60
# File 'lib/image_optim/image_path.rb', line 58

def binread
  open('rb', &:read)
end

#copy(dst) ⇒ Object

Copy file to dest preserving attributes



36
37
38
# File 'lib/image_optim/image_path.rb', line 36

def copy(dst)
  FileUtils.copy_file(self, dst, true)
end

#formatObject

Get format using ImageSize



52
53
54
55
# File 'lib/image_optim/image_path.rb', line 52

def format
  image_meta = ImageMeta.for_path(self)
  image_meta && image_meta.format
end

#replace(src) ⇒ Object

Atomic replace src with self



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

def replace(src)
  src = self.class.new(src)
  src.temp_path(src.dirname) do |temp|
    src.copy(temp)
    temp.write(read)
    temp.rename(src.to_s)
    unlink
  end
end

#temp_path(*args, &block) ⇒ Object

Get temp path for this file with same extension



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

def temp_path(*args, &block)
  ext = extname
  self.class.temp_file_path([basename(ext).to_s, ext], *args, &block)
end