Skip to content
This repository
Browse code

Remove method missing use in respond_to

  • Loading branch information...
commit 6dc12881110d26bb952bd0f565623144f10a07b6 1 parent db5a98e
Yehuda Katz authored December 26, 2008
27  actionpack/lib/action_controller/mime_responds.rb
@@ -143,12 +143,31 @@ def any(*args, &block)
143 143
           custom(@mime_type_priority.first, &block)
144 144
         end
145 145
       end
  146
+      
  147
+      def self.generate_method_for_mime(mime)
  148
+        sym = mime.is_a?(Symbol) ? mime : mime.to_sym
  149
+        const = sym.to_s.upcase
  150
+        class_eval <<-RUBY
  151
+          def #{sym}(&block)                          # def html(&block)
  152
+            if Mime::SET.include?(Mime::#{const})     #   if Mime::Set.include?(Mime::HTML)
  153
+              custom(Mime::#{const}, &block)          #     custom(Mime::HTML, &block)
  154
+            else                                      #   else
  155
+              super                                   #     super
  156
+            end                                       #   end
  157
+          end                                         # end
  158
+        RUBY
  159
+      end
146 160
 
147  
-      def method_missing(symbol, &block)
148  
-        mime_constant = symbol.to_s.upcase
  161
+      Mime::SET.each do |mime|
  162
+        generate_method_for_mime(mime)
  163
+      end
149 164
 
150  
-        if Mime::SET.include?(Mime.const_get(mime_constant))
151  
-          custom(Mime.const_get(mime_constant), &block)
  165
+      def method_missing(symbol, &block)
  166
+        mime_constant = Mime.const_get(symbol.to_s.upcase)
  167
+      
  168
+        if Mime::SET.include?(mime_constant)
  169
+          self.class.generate_method_for_mime(mime_constant)
  170
+          send(symbol, &block)
152 171
         else
153 172
           super
154 173
         end

13 notes on commit 6dc1288

Pratik
Owner

Congrats on the first commit :-)

Geoffrey Grosenbach

He gets straight to business!

Dr Nic Williams

Weee

Dr Nic Williams

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

Scott Taylor

Shouldn’t you make method_missing private?

Michael Klishin

@drnic

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

Nathaniel Bibler

+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.

Geoffrey Grosenbach

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

Chad Woolley

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…

Jeremy Pinnix

+1 for inline meta comments

Xavier Noria
Owner

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

Kirk Richey

This is an awesome blossom.

Michael Koziarski
Owner

OMG lulz CONGRATS!!!

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

Please sign in to comment.
Something went wrong with that request. Please try again.