Class: Miso::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/miso/factory.rb

Overview

A Miso::Image::Factory can be used to define multiple operations which are to be applied to an image.

A typical use case would be:

class Member < ActiveRecord::Base
  has_variant :avatar, :processor => Miso::Image.factory.crop(123, 456).watermark(WATERMARK, :southwest, 10, 10)
end

In this example, the resulting Miso::Factory instance would be used by the attachment library to apply the operations. Eg:

module HasVariant
  def process_variant(name)
    variant = self.class.variants[name]
    variant.processor.apply(upload_file, output_file)
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(processor_class = Processor.processor_class) ⇒ Factory

Returns a new instance of Factory.



27
28
29
30
# File 'lib/miso/factory.rb', line 27

def initialize(processor_class = Processor.processor_class)
  @processor_class = processor_class
  @operations = []
end

Instance Method Details

#apply(input_file, output_file) ⇒ Object



52
53
54
55
56
# File 'lib/miso/factory.rb', line 52

def apply(input_file, output_file)
  image = Image.new(input_file, @processor_class)
  @operations.each { |method, args| image.send(method, *args) }
  image.write(output_file)
end

#auto_orientObject



47
48
49
50
# File 'lib/miso/factory.rb', line 47

def auto_orient
  @operations << [:auto_orient, []]
  self
end

#crop(width, height) ⇒ Object



32
33
34
35
# File 'lib/miso/factory.rb', line 32

def crop(width, height)
  @operations << [:crop, [width, height]]
  self
end

#crop_fitting(width, height) ⇒ Object



42
43
44
45
# File 'lib/miso/factory.rb', line 42

def crop_fitting(width, height)
  @operations << [:crop_fitting, [width, height]]
  self
end

#fit(width, height) ⇒ Object



37
38
39
40
# File 'lib/miso/factory.rb', line 37

def fit(width, height)
  @operations << [:fit, [width, height]]
  self
end