Class: ImageOptim::Handler

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

Overview

Handles processing of original to result using upto two temp files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original) ⇒ Handler

original must respond to temp_path



10
11
12
13
14
15
16
17
# File 'lib/image_optim/handler.rb', line 10

def initialize(original)
  unless original.respond_to?(:temp_path)
    fail ArgumentError, 'original should respond to temp_path'
  end

  @original = original
  @result = nil
end

Instance Attribute Details

#resultObject (readonly)

Holds latest successful result



7
8
9
# File 'lib/image_optim/handler.rb', line 7

def result
  @result
end

Instance Method Details

#cleanupObject

Remove extra temp files



35
36
37
38
39
# File 'lib/image_optim/handler.rb', line 35

def cleanup
  return unless @dst
  @dst.unlink
  @dst = nil
end

#processObject

Yields two paths, one to latest successful result or original, second to temp path



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/image_optim/handler.rb', line 21

def process
  @src ||= @original
  @dst ||= @original.temp_path

  return unless yield @src, @dst
  @result = @dst
  if @src == @original
    @src, @dst = @dst, nil
  else
    @src, @dst = @dst, @src
  end
end