Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove method missing use in respond_to

  • Loading branch information...
commit 6dc12881110d26bb952bd0f565623144f10a07b6 1 parent db5a98e
@wycats wycats authored
Showing with 23 additions and 4 deletions.
  1. +23 −4 actionpack/lib/action_controller/mime_responds.rb
View
27 actionpack/lib/action_controller/mime_responds.rb
@@ -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

@lifo
Collaborator

Congrats on the first commit :-)

@topfunky

He gets straight to business!

@drnic

Weee

@drnic

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

@smtlaissezfaire

Shouldn’t you make method_missing private?

@michaelklishin

@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

+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

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

@thewoolleyman

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

+1 for inline meta comments

@fxn
Owner

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

@kirk

This is an awesome blossom.

@NZKoz
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.