class Guard::ImageOptim

Public Class Methods

new(options = {}) click to toggle source

Initializer

Calls superclass method
# 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

Public Instance Methods

reload() click to toggle source

On Guard reload

# File lib/guard/image_optim.rb, line 57
def reload
    run_all
end
run_all() click to toggle source

Run all

# 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) click to toggle source

Run on changes to watched files

@param {Array} paths

Paths of changed files
# 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) click to toggle source

Called when a watched file is removed

@param {Array} paths

Paths of changed files
# File lib/guard/image_optim.rb, line 86
def run_on_removals(paths)
end
start() click to toggle source

Run at start

# File lib/guard/image_optim.rb, line 43
def start
    run_all if options[:run_at_start]
end
stop() click to toggle source

Stop running

# File lib/guard/image_optim.rb, line 50
def stop
    true
end

Private Instance Methods

color(message, color) click to toggle source

Set message color

@param {String} message

Text to color

@param {String} color

Color code
# File lib/guard/image_optim.rb, line 113
def color(message, color)
    if ::Guard::UI.send(:color_enabled?)
        "\e[0#{color}m#{message}\e[0m"
    else
        message
    end
end
optimize!(file) click to toggle source

Optimize file

@param {String} file

# File lib/guard/image_optim.rb, line 96
def optimize!(file)
    begin
        throw :task_has_failed unless @optimizer.optimize_image! file
        ::Guard::UI.info(color("optimized #{File.basename(file)}", ";32")) if options[:notifications]
    rescue StandardError => error
        ::Guard::UI.error(color("error optimizing #{File.basename(file)} : #{error}", ";31")) if options[:notifications]
    end
end