diff options
author | George Claghorn <george.claghorn@gmail.com> | 2018-08-10 12:10:13 -0400 |
---|---|---|
committer | George Claghorn <george.claghorn@gmail.com> | 2018-08-10 12:42:14 -0400 |
commit | 697f4a93ad386f9fb7795f0ba68f815f16ebad0f (patch) | |
tree | 57576c80df328be9b910dade1b21faddf8798629 /activestorage/lib/active_storage/transformers/transformer.rb | |
parent | 924f44371697c5fe1509a749e1187ad2c9257ab9 (diff) | |
download | rails-697f4a93ad386f9fb7795f0ba68f815f16ebad0f.tar.gz rails-697f4a93ad386f9fb7795f0ba68f815f16ebad0f.tar.bz2 rails-697f4a93ad386f9fb7795f0ba68f815f16ebad0f.zip |
Extract transformers
Diffstat (limited to 'activestorage/lib/active_storage/transformers/transformer.rb')
-rw-r--r-- | activestorage/lib/active_storage/transformers/transformer.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activestorage/lib/active_storage/transformers/transformer.rb b/activestorage/lib/active_storage/transformers/transformer.rb new file mode 100644 index 0000000000..2e21201004 --- /dev/null +++ b/activestorage/lib/active_storage/transformers/transformer.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module ActiveStorage + module Transformers + # A Transformer applies a set of transformations to an image. + # + # The following concrete subclasses are included in Active Storage: + # + # * ActiveStorage::Transformers::ImageProcessingTransformer: + # backed by ImageProcessing, a common interface for MiniMagick and ruby-vips + # + # * ActiveStorage::Transformers::MiniMagickTransformer: + # backed by MiniMagick, a wrapper around the ImageMagick CLI + class Transformer + attr_reader :transformations + + def initialize(transformations) + @transformations = transformations + end + + # Applies the transformations to the source image in +file+, producing a target image in the + # specified +format+. Yields an open Tempfile containing the target image. Closes and unlinks + # the output tempfile after yielding to the given block. Returns the result of the block. + def transform(file, format:) + output = process(file, format: format) + + begin + yield output + ensure + output.close! + end + end + + private + # Returns an open Tempfile containing a transformed image in the given +format+. + # All subclasses implement this method. + def process(file, format:) #:doc: + raise NotImplementedError + end + end + end +end |