Class: ImageOptim::BinResolver::Bin

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

Overview

Holds bin name and path, gets version

Defined Under Namespace

Classes: BadVersion, UnknownVersion

Constant Summary collapse

FAIL_CHECKS =
[
  [:pngcrush, is.between?('1.7.60', '1.7.65'), 'is known to produce '\
        'broken pngs'],
  [:pngcrush, is == '1.7.80', 'loses one color in indexed images'],
  [:pngquant, is < '2.0', 'is not supported'],
]
WARN_CHECKS =
[
  [:advpng, is < '1.17', 'does not use zopfli'],
  [:gifsicle, is < '1.85', 'does not support removing extension blocks'],
  [:pngcrush, is < '1.7.38', 'does not have blacken flag'],
  [:pngquant, is < '2.1', 'may be lossy even with quality `100-`'],
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path) ⇒ Bin

Returns a new instance of Bin.



15
16
17
18
19
# File 'lib/image_optim/bin_resolver/bin.rb', line 15

def initialize(name, path)
  @name = name.to_sym
  @path = path.to_s
  @version = detect_version
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/image_optim/bin_resolver/bin.rb', line 14

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/image_optim/bin_resolver/bin.rb', line 14

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/image_optim/bin_resolver/bin.rb', line 14

def version
  @version
end

Instance Method Details

#check!Object

Run check_fail!, otherwise warn if version is known to misbehave



53
54
55
56
57
58
59
60
61
# File 'lib/image_optim/bin_resolver/bin.rb', line 53

def check!
  check_fail!

  WARN_CHECKS.each do |bin_name, matcher, message|
    next unless bin_name == name
    next unless matcher.match(version)
    warn "WARN: #{self} (#{matcher}) #{message}"
  end
end

#check_fail!Object

Fail if version will not work properly



42
43
44
45
46
47
48
49
50
# File 'lib/image_optim/bin_resolver/bin.rb', line 42

def check_fail!
  fail UnknownVersion, "didn't get version of #{self}" unless version

  FAIL_CHECKS.each do |bin_name, matcher, message|
    next unless bin_name == name
    next unless matcher.match(version)
    fail BadVersion, "#{self} (#{matcher}) #{message}"
  end
end

#to_sObject



21
22
23
# File 'lib/image_optim/bin_resolver/bin.rb', line 21

def to_s
  "#{name} #{version || '?'} at #{path}"
end