public
Description: Rails plugin for uploading images as resources, with support for resizing, text stamping, and other special effects.
Homepage: http://wiki.github.com/Squeegy/fleximage
Clone URL: git://github.com/Squeegy/fleximage.git
ImageProxy#method_missing no longer directly executes operators.  Instead, it 
defines a method for that operator, then immediately calls that method.  Future 
calls for that operator are now handled by the ruby method, not method missing.  
This keeps the dynamic ruby metaprogramming mumbo jumbo to only the first time 
you call an operator, and from then on its as as fast as calling a standard ruby 
method.
Squeegy (author)
Wed May 21 13:06:00 -0700 2008
commit  58fd79dcf816d9417e2c78914fd02f2de5f74d3a
tree    3a7d69b06106d9e5e7366a7c5c71a17647c4de34
parent  81e51e7489ee4e397574a624d0eea432c675677d
...
30
31
32
33
34
 
 
 
 
 
 
 
 
 
35
36
37
...
40
41
42
 
 
 
 
 
 
 
43
44
45
46
...
30
31
32
 
 
33
34
35
36
37
38
39
40
41
42
43
44
...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
0
@@ -30,8 +30,15 @@ module Fleximage
0
       class_name = method_name.to_s.camelcase
0
       operator_class = "Fleximage::Operator::#{class_name}".constantize
0
       
0
-      # Execute the operator
0
-      @image = operator_class.new(self, @image, @model).execute(*args)
0
+      # Define a method for this operator so future calls to this operation are faster
0
+      self.class.module_eval <<-EOF
0
+        def #{method_name}(*args)
0
+          @image = execute_operator(#{operator_class}, *args)
0
+        end
0
+      EOF
0
+      
0
+      # Call the method that was just defined to perform its functionality.
0
+      send(method_name, *args)
0
     
0
     rescue NameError => e
0
       if e.to_s =~ /uninitialized constant Fleximage::Operator::#{class_name}/
0
@@ -40,6 +47,13 @@ module Fleximage
0
         raise e
0
       end
0
     end
0
+    
0
+    private
0
+      # Instantiate and execute the requested image Operator.
0
+      def execute_operator(operator_class, *args)
0
+        operator_class.new(self, @image, @model).execute(*args)
0
+      end
0
+    
0
   end
0
   
0
 end
0
\ No newline at end of file
...
315
316
317
 
 
318
319
 
320
321
322
...
447
448
449
450
 
451
452
453
...
315
316
317
318
319
320
321
322
323
324
325
...
450
451
452
 
453
454
455
456
0
@@ -315,8 +315,11 @@ module Fleximage
0
       # processed output image.
0
       def load_image #:nodoc:
0
         @output_image ||= @uploaded_image
0
+        
0
+        # Return the current image if we have loaded it already
0
         return @output_image if @output_image
0
         
0
+        # Load the image from disk
0
         if self.class.db_store?
0
           if image_file_data && image_file_data.any?
0
             @output_image = Magick::Image.from_blob(image_file_data).first
0
@@ -447,7 +450,7 @@ module Fleximage
0
           
0
           # No default, not master image, so raise exception
0
           else
0
-            message = "Master image was not found for this record, so no image can be rendered."
0
+            message = "Master image was not found for this record"
0
             
0
             if !self.class.db_store?
0
               message << "\nExpected image to be at:"
...
29
30
31
 
32
33
 
34
35
36
37
38
 
39
40
41
...
60
61
62
63
 
 
64
65
 
 
66
67
 
 
68
 
69
70
71
72
73
74
75
76
 
 
77
78
79
 
80
81
82
83
84
85
86
87
 
 
88
89
90
91
92
93
94
 
 
95
96
97
...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
...
63
64
65
 
66
67
68
 
69
70
71
 
72
73
74
75
76
77
78
79
80
81
 
 
82
83
84
85
 
86
87
88
89
90
91
92
 
 
93
94
95
96
97
98
99
 
 
100
101
102
103
104
0
@@ -29,13 +29,16 @@ module Fleximage
0
       
0
       # Start the operation
0
       def execute(*args) #:nodoc:
0
+        # Get the result of the Operators #operate method
0
         result = operate(*args)
0
         
0
+        # Ensure that the result is an RMagick:Image object
0
         unless result.is_a?(Magick::Image)
0
           raise BadOperatorResult, "expected #{self.class}#operate to return an instance of Magick::Image. \n"+
0
                                    "Got instance of #{result.class} instead."
0
         end
0
         
0
+        # Save the result to the operator proxy
0
         @proxy.image = result
0
       end
0
       
0
@@ -60,38 +63,42 @@ module Fleximage
0
       #
0
       #   x, y = size_to_xy("10x20")
0
       def size_to_xy(size)
0
-        if size.is_a?(Array) && size.size == 2
0
+        case          
0
+        when size.is_a?(Array) && size.size == 2  # [320, 240]
0
           size
0
-        elsif size.to_s.include?('x')
0
+      
0
+        when size.to_s.include?('x')              # "320x240"
0
           size.split('x').collect(&:to_i)
0
-        else
0
+        
0
+        else # Anything else, convert the object to an integer and assume square dimensions
0
           [size.to_i, size.to_i]
0
+          
0
         end
0
       end
0
       
0
       # Scale the image, respecting aspect ratio.  
0
       # Operation will happen in the main <tt>@image</tt> unless you supply the +img+ argument
0
       # to operate on instead.
0
-      def scale(size, img = nil)
0
-        (img || @image).change_geometry!(size_to_xy(size).join('x')) do |cols, rows, img|
0
+      def scale(size, img = @image)
0
+        img.change_geometry!(size_to_xy(size).join('x')) do |cols, rows, _img|
0
           cols = 1 if cols < 1
0
           rows = 1 if rows < 1
0
-          img.resize!(cols, rows)
0
+          _img.resize!(cols, rows)
0
         end
0
       end
0
       
0
       # Scale to the desired size and crop edges off to get the exact dimensions needed.
0
       # Operation will happen in the main <tt>@image</tt> unless you supply the +img+ argument
0
       # to operate on instead.
0
-      def scale_and_crop(size, img = nil)
0
-        (img || @image).crop_resized!(*size_to_xy(size))
0
+      def scale_and_crop(size, img = @image)
0
+        img.crop_resized!(*size_to_xy(size))
0
       end
0
       
0
       # Resize the image, with no respect to aspect ratio.  
0
       # Operation will happen in the main <tt>@image</tt> unless you supply the +img+ argument
0
       # to operate on instead.
0
-      def stretch(size, img = nil)
0
-        (img || @image).resize!(*size_to_xy(size))
0
+      def stretch(size, img = @image)
0
+        img.resize!(*size_to_xy(size))
0
       end
0
       
0
       # Convert a symbol to an RMagick blending mode.

Comments