Class: Guard::ImageOptim

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ImageOptim

Initializer



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/guard/image_optim.rb', line 17

def initialize(options = {})
    options = {
        :input => "images",
        :nice => 10,
        :notifications => true,
        :run_at_start => false,
        :pngout => false,
        :svgo => false
    }.merge(options)

    @optimizer = ::ImageOptim.new({
        :nice => options[:nice],
        :pngout => options[:pngout],
        :svgo => options[:svgo]
    })

    super(options)

    if options[:input]
        watchers << ::Guard::Watcher.new(%r{^#{options[:input]}/*.*$})
    end
end

Instance Method Details

#reloadObject

On Guard reload



57
58
59
# File 'lib/guard/image_optim.rb', line 57

def reload
    run_all
end

#run_allObject

Run all



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

def run_all
    run_on_changes Watcher.match_files(self, Dir.glob(File.join("**", "*.*")))
end

#run_on_changes(paths) ⇒ Object

Run on changes to watched files

Parameters:

  • paths (Array)

    Paths of changed files



74
75
76
77
78
# File 'lib/guard/image_optim.rb', line 74

def run_on_changes(paths)
    paths.each do |file|
        optimize! Pathname.new(file).realpath
    end
end

#run_on_removals(paths) ⇒ Object

Called when a watched file is removed

Parameters:

  • paths (Array)

    Paths of changed files



86
87
# File 'lib/guard/image_optim.rb', line 86

def run_on_removals(paths)
end

#startObject

Run at start



43
44
45
# File 'lib/guard/image_optim.rb', line 43

def start
    run_all if options[:run_at_start]
end

#stopObject

Stop running



50
51
52
# File 'lib/guard/image_optim.rb', line 50

def stop
    true
end