Skip to content

Commit

Permalink
Remove method missing use in respond_to
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Dec 26, 2008
1 parent db5a98e commit 6dc1288
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions actionpack/lib/action_controller/mime_responds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

13 comments on commit 6dc1288

@lifo
Copy link
Member

@lifo lifo commented on 6dc1288 Dec 26, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats on the first commit :-)

@topfunky
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He gets straight to business!

@drnic
Copy link
Contributor

@drnic drnic commented on 6dc1288 Dec 26, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weee

@drnic
Copy link
Contributor

@drnic drnic commented on 6dc1288 Dec 26, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@smtlaissezfaire
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t you make method_missing private?

@michaelklishin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@thewoolleyman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for inline meta comments

@fxn
Copy link
Member

@fxn fxn commented on 6dc1288 Dec 27, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@kirk
Copy link

@kirk kirk commented on 6dc1288 Dec 27, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome blossom.

@NZKoz
Copy link
Member

@NZKoz NZKoz commented on 6dc1288 Dec 28, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG lulz CONGRATS!!!

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

Please sign in to comment.