Sha256: 25c876cd00794e65a46e2d8fdfbc3aca6dfb362ea8e99ac69fa3d055d2d9021b

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

module Paperdragon
  # Gives a simple API for processing multiple versions of a single attachment.
  class Task
    def initialize(attachment, upload=nil)
      @attachment = attachment
      @upload     = upload
      @metadata   = attachment.metadata.dup # DISCUSS: keep this dependency?

      yield self if block_given?
    end

    attr_reader :metadata
    def metadata_hash # semi-private, might be removed.
      metadata.to_hash
    end

    # process!(style, [*args,] &block) :
    #   version = CoverGirl::Photo.new(@model, style, *args)
    #   metadata = version.process!(upload, &block)
    #   merge! {style => metadata}
    def process!(style, &block)
      @metadata.merge!(style => file(style, upload).process!(upload, &block))
    end

    # fingerprint optional => filename is gonna remain the same
    # original nil => use [:original]
    def reprocess!(style, fingerprint=nil, original=nil, &block)
      @original ||= file(:original) # this is cached per task instance.
      version     = file(style)
      new_uid     = @attachment.rebuild_uid(version, fingerprint)

      @metadata.merge!(style => version.reprocess!(new_uid, @original, &block))
    end

    def rename!(style, fingerprint, &block)
      version = file(style)
      new_uid = @attachment.rebuild_uid(version, fingerprint)

      @metadata.merge!(style => version.rename!(new_uid, &block))
    end

  private
    def file(style, upload=nil)
      @attachment[style, upload]
    end

    def upload
      @upload or raise MissingUploadError.new("You called #process! but didn't pass an uploaded file to Attachment#task.")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paperdragon-0.0.6 lib/paperdragon/task.rb
paperdragon-0.0.5 lib/paperdragon/task.rb
paperdragon-0.0.4 lib/paperdragon/task.rb