Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove method missing use in respond_to

  • Loading branch information...
commit 6dc12881110d26bb952bd0f565623144f10a07b6 1 parent db5a98e
Yehuda Katz wycats authored
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
View
@@ -143,12 +143,31 @@ def any(*args, &block)
custom(@mime_type_priority.first, &block)
end
end
+
+ 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
+ RUBY
+ 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)
else
super
end

13 comments on commit 6dc1288

Pratik
Collaborator

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.