Class: ImageOptim::Handler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original) ⇒ Handler

Returns a new instance of Handler.

Raises:

  • (ArgumentError)


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

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

  @original = original
  @result = nil
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/image_optim/handler.rb', line 5

def result
  @result
end

Instance Method Details

#cleanupObject

Remove extra temp files



28
29
30
31
32
33
# File 'lib/image_optim/handler.rb', line 28

def cleanup
  if @dst
    @dst.unlink
    @dst = nil
  end
end

#processObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/image_optim/handler.rb', line 13

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

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