Class: Jekyll::Images::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/images/runner.rb

Overview

Runner for optimizations

Direct Known Subclasses

JpegOptim, Oxipng

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, interlaced = false) ⇒ Runner

Returns a new instance of Runner.



37
38
39
40
41
# File 'lib/jekyll/images/runner.rb', line 37

def initialize(file, interlaced = false)
  @file = file
  @bytes_before = bytes_after
  @interlaced = interlaced
end

Instance Attribute Details

#binaryObject (readonly)

Returns the value of attribute binary.



10
11
12
# File 'lib/jekyll/images/runner.rb', line 10

def binary
  @binary
end

#bytes_afterObject (readonly)

Returns the value of attribute bytes_after.



10
11
12
# File 'lib/jekyll/images/runner.rb', line 10

def bytes_after
  @bytes_after
end

#bytes_beforeObject (readonly)

Returns the value of attribute bytes_before.



10
11
12
# File 'lib/jekyll/images/runner.rb', line 10

def bytes_before
  @bytes_before
end

#fileObject (readonly)

Returns the value of attribute file.



10
11
12
# File 'lib/jekyll/images/runner.rb', line 10

def file
  @file
end

#interlacedObject (readonly)

Returns the value of attribute interlaced.



10
11
12
# File 'lib/jekyll/images/runner.rb', line 10

def interlaced
  @interlaced
end

Class Method Details

.mime(file) ⇒ Object



25
26
27
28
29
# File 'lib/jekyll/images/runner.rb', line 25

def mime(file)
  @@mime ||= FileMagic.new

  @@mime.file(file, true)
end

.register(mime, class_name) ⇒ Object

XXX: This only allows one optimization per file type, we could make it an array but how do we garantee order? Maybe adding a priority field?



16
17
18
19
# File 'lib/jekyll/images/runner.rb', line 16

def register(mime, class_name)
  @@runners ||= {}
  @@runners[mime] = class_name
end

.run(file, interlaced = false) ⇒ Object



31
32
33
34
# File 'lib/jekyll/images/runner.rb', line 31

def run(file, interlaced = false)
  type = mime(file)
  runners[type].new(file, interlaced).run if runners[type]
end

.runnersObject



21
22
23
# File 'lib/jekyll/images/runner.rb', line 21

def runners
  @@runners
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/jekyll/images/runner.rb', line 47

def exist?
  @binary, _, status = Open3.capture3("which #{self.class::BINARY}")
  @binary.chomp!

  status.success?
end

#runObject



54
55
56
57
58
59
60
# File 'lib/jekyll/images/runner.rb', line 54

def run
  return unless exist?

  _, status = Open3.capture2e(*command)

  [bytes_before, bytes_after] if status.success?
end