Class: Fleximage::ImageProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/fleximage/image_proxy.rb

Overview

An instance of this class is yielded when Model#operate is called. It enables image operators to be called to transform the image. You should never need to directly deal with this class. You simply call image operators on this object when inside an Model#operate block

@photo.operate do |image|
  image.resize '640x480'
end

In this example, image is an instance of ImageProxy

Defined Under Namespace

Classes: OperatorNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, model_obj) ⇒ ImageProxy

Create a new image operator proxy.



21
22
23
24
# File 'lib/fleximage/image_proxy.rb', line 21

def initialize(image, model_obj)
  @image = image
  @model = model_obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

A call to an unknown method will look for an Operator by that method’s name. If it finds one, it will execute that operator.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fleximage/image_proxy.rb', line 38

def method_missing(method_name, *args)
  # Find the operator class
  class_name = method_name.to_s.camelcase
  operator_class = "Fleximage::Operator::#{class_name}".constantize
  
  # Define a method for this operator so future calls to this operation are faster
  self.class.module_eval <<-EOF
    def #{method_name}(*args)
      @image = execute_operator(#{operator_class}, *args)
    end
  EOF
  
  # Call the method that was just defined to perform its functionality.
  send(method_name, *args)

rescue NameError => e
  if e.to_s =~ /uninitialized constant Fleximage::Operator::#{class_name}/
    raise OperatorNotFound, "No operator Fleximage::Operator::#{class_name} found for the method \"#{method_name}\""
  else
    raise e
  end
end

Instance Attribute Details

#imageObject

The image to be manipulated by operators.



18
19
20
# File 'lib/fleximage/image_proxy.rb', line 18

def image
  @image
end

Instance Method Details

#heightObject

Shortcut for accessing current image height



32
33
34
# File 'lib/fleximage/image_proxy.rb', line 32

def height
  @image.rows
end

#widthObject

Shortcut for accessing current image width



27
28
29
# File 'lib/fleximage/image_proxy.rb', line 27

def width
  @image.columns
end