Browse files

Remove method missing use in respond_to

  • Loading branch information...
1 parent db5a98e commit 6dc12881110d26bb952bd0f565623144f10a07b6 @wycats wycats committed Dec 26, 2008
Showing with 23 additions and 4 deletions.
  1. +23 −4 actionpack/lib/action_controller/mime_responds.rb
27 actionpack/lib/action_controller/mime_responds.rb
@@ -143,12 +143,31 @@ def any(*args, &block)
custom(@mime_type_priority.first, &block)
+ def self.generate_method_for_mime(mime)
+ sym = mime.is_a?(Symbol) ? mime : mime.to_sym
+ const = sym.to_s.upcase
+ class_eval <<-RUBY
+ def #{sym}(&block) # def html(&block)
+ if Mime::SET.include?(Mime::#{const}) # if Mime::Set.include?(Mime::HTML)
+ custom(Mime::#{const}, &block) # custom(Mime::HTML, &block)
+ else # else
+ super # super
+ end # end
+ end # end
+ end
- def method_missing(symbol, &block)
- mime_constant = symbol.to_s.upcase
+ Mime::SET.each do |mime|
+ generate_method_for_mime(mime)
+ end
- if Mime::SET.include?(Mime.const_get(mime_constant))
- custom(Mime.const_get(mime_constant), &block)
+ def method_missing(symbol, &block)
+ mime_constant = Mime.const_get(symbol.to_s.upcase)
+ if Mime::SET.include?(mime_constant)
+ self.class.generate_method_for_mime(mime_constant)
+ send(symbol, &block)

13 comments on commit 6dc1288

Ruby on Rails member

Congrats on the first commit :-)


He gets straight to business!




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


Shouldn’t you make method_missing private?



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


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


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


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…


+1 for inline meta comments

Ruby on Rails member

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


This is an awesome blossom.

Ruby on Rails member


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

Please sign in to comment.