Class: AssetImageOpt

Inherits:
Object
  • Object
show all
Defined in:
lib/asset-image-opt.rb

Constant Summary collapse

EXTENSIONS =
%w{png jpeg jpg gif}
WORKING_DIR =
"app/assets/images"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAssetImageOpt

Returns a new instance of AssetImageOpt.



11
12
13
14
15
# File 'lib/asset-image-opt.rb', line 11

def initialize
  @files = AssetImageOpt.get_files
  @initial_size = get_files_size
  @optimizer = ImageOptim.new(:pngout => false)
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



9
10
11
# File 'lib/asset-image-opt.rb', line 9

def files
  @files
end

#initial_sizeObject

Returns the value of attribute initial_size.



9
10
11
# File 'lib/asset-image-opt.rb', line 9

def initial_size
  @initial_size
end

#optimized_sizeObject

Returns the value of attribute optimized_size.



9
10
11
# File 'lib/asset-image-opt.rb', line 9

def optimized_size
  @optimized_size
end

Class Method Details

.get_filesObject



66
67
68
69
70
71
72
# File 'lib/asset-image-opt.rb', line 66

def self.get_files
  res = []
  EXTENSIONS.each do |ext|
    res += AssetImageOpt.get_files_for_ext(ext)
  end
  res
end

.get_files_for_ext(ext) ⇒ Object



62
63
64
# File 'lib/asset-image-opt.rb', line 62

def self.get_files_for_ext(ext)
  Dir.glob("#{Dir.pwd}/#{WORKING_DIR}/**/*.#{ext}")
end

Instance Method Details

#get_files_sizeObject



79
80
81
82
83
84
85
# File 'lib/asset-image-opt.rb', line 79

def get_files_size
  size = 0
  @files.each do |file|
    size += File.size(file)
  end
  size
end

#optimizeObject



53
54
55
56
57
58
59
60
# File 'lib/asset-image-opt.rb', line 53

def optimize
  if @files.empty?
    puts "No files to optimize"
  else
    puts "Optimizing images"
    optimize_files
  end
end

#optimize_file(file) ⇒ Object



74
75
76
77
# File 'lib/asset-image-opt.rb', line 74

def optimize_file(file)
  @optimizer.optimize_image!(file)
  print '.'
end

#optimize_filesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/asset-image-opt.rb', line 35

def optimize_files
  queue = Queue.new
  @files.each{|e| queue << e }

  threads = []
  threads_count.times do
    threads << Thread.new do
      while (e = queue.pop(true) rescue nil)
        optimize_file(e)
      end
    end
  end

  threads.each {|t| t.join }
  puts ""
  @optimized_size = get_files_size
end

#threads_countObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/asset-image-opt.rb', line 17

def threads_count
  case RbConfig::CONFIG['host_os']
  when /darwin9/
    `hwprefs cpu_count`.to_i
  when /darwin/
    ((`which hwprefs` != '') ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
  when /linux/
    `cat /proc/cpuinfo | grep processor | wc -l`.to_i
  when /freebsd/
    `sysctl -n hw.ncpu`.to_i
  when /mswin|mingw/
    require 'win32ole'
    wmi = WIN32OLE.connect("winmgmts://")
    cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # TODO count hyper-threaded in this
    cpu.to_enum.first.NumberOfCores
  end
end