# file : lib/paperclip/attachment.rb 
#
# here is a simple approach/modification to paperclip which allows mutiple the
# thumbnails to be generated in parallel.  ideally you'd want one thumbnail
# being generated per-processor; since most machines these days have at least
# two processors the code below uses that as a parameter
#
#
  require 'threadify'

  module Paperclip
    class Attachment
      def post_process_styles
        @styles.each do |name, args|
          raise RuntimeError.new("Style #{name} has no processors defined.") if args[:processors].blank?
        end

        begin
          results =
            @styles.threadify(number_of_cpus = 2) do |name, args|
              file = @queued_for_write[:original]
              args[:processors].map do |processor|
                file = Paperclip.processor(processor).make(file, args, self)
              end
              {name => file}
            end
          results.each{|result| @queued_for_write.update(result)}
        rescue PaperclipError => e
          log("An error was received while processing: #{e.inspect}")
          (@errors[:processing] ||= []) << e.message if @whiny
        end
      end
    end
  end