public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Remove method missing use in respond_to
wycats (author)
Fri Dec 26 13:37:42 -0800 2008
commit  6dc12881110d26bb952bd0f565623144f10a07b6
tree    68c1a4cf50cc28864cf2b72714508e2524798a62
parent  db5a98e6cbb88331a6ce484260e9cce9ba882bcd
...
143
144
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
147
148
 
 
 
149
150
151
 
 
 
 
 
 
152
153
154
...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 
 
161
162
163
164
 
 
165
166
167
168
169
170
171
172
173
0
@@ -143,12 +143,31 @@ module ActionController #:nodoc:
0
           custom(@mime_type_priority.first, &block)
0
         end
0
       end
0
+      
0
+      def self.generate_method_for_mime(mime)
0
+        sym = mime.is_a?(Symbol) ? mime : mime.to_sym
0
+        const = sym.to_s.upcase
0
+        class_eval <<-RUBY
0
+          def #{sym}(&block)                          # def html(&block)
0
+            if Mime::SET.include?(Mime::#{const})     #   if Mime::Set.include?(Mime::HTML)
0
+              custom(Mime::#{const}, &block)          #     custom(Mime::HTML, &block)
0
+            else                                      #   else
0
+              super                                   #     super
0
+            end                                       #   end
0
+          end                                         # end
0
+        RUBY
0
+      end
0
 
0
-      def method_missing(symbol, &block)
0
-        mime_constant = symbol.to_s.upcase
0
+      Mime::SET.each do |mime|
0
+        generate_method_for_mime(mime)
0
+      end
0
 
0
-        if Mime::SET.include?(Mime.const_get(mime_constant))
0
-          custom(Mime.const_get(mime_constant), &block)
0
+      def method_missing(symbol, &block)
0
+        mime_constant = Mime.const_get(symbol.to_s.upcase)
0
+      
0
+        if Mime::SET.include?(mime_constant)
0
+          self.class.generate_method_for_mime(mime_constant)
0
+          send(symbol, &block)
0
         else
0
           super
0
         end

Comments

lifo Fri Dec 26 13:42:25 -0800 2008

Congrats on the first commit :-)

topfunky Fri Dec 26 13:51:40 -0800 2008

He gets straight to business!

drnic Fri Dec 26 13:52:04 -0800 2008

Weee

drnic Fri Dec 26 13:53:00 -0800 2008

btw, I really like the inline docco sample of the generated method that will be created by #generate_method_for_mime

smtlaissezfaire Fri Dec 26 14:09:57 -0800 2008

Shouldn’t you make method_missing private?

michaelklishin Fri Dec 26 16:23:30 -0800 2008

@drnic

I hope from this point, every metaprogramming trick will have such comment. I personally find it very hard to read metaprogramming code otherwise.

nbibler Fri Dec 26 20:42:17 -0800 2008

+1 for human-readable meta comment. It’d certainly be nice to have this used throughout the code base. Great job… and nice first commit.

topfunky Fri Dec 26 20:56:35 -0800 2008

Expectations are high for wycats’ second commit to Rails. We’ll be watching for it with baited breath.

thewoolleyman Fri Dec 26 22:10:03 -0800 2008

I’m waiting for the failing test which prevents method_missing from ever being used :)

The inline sample doc is really nice too, sets great precedent. I wonder if there could be a failing test to enforce that too – any call to def inside a class eval must have a trailing comment…

jpinnix Sat Dec 27 07:28:56 -0800 2008

+1 for inline meta comments

fxn Sat Dec 27 08:13:30 -0800 2008

Those comments are a great idea. Inspired by this commit I’m adding them in more places through docrails.

kirk Sat Dec 27 09:24:34 -0800 2008

This is an awesome blossom.

NZKoz Sun Dec 28 10:13:50 -0800 2008

OMG lulz CONGRATS!!!

In all seriousness, the commented code is cool, more plz