Created
May 20, 2011 15:47
-
-
Save bgarret/983207 to your computer and use it in GitHub Desktop.
Redefine responds_to_event to allow preventing event bubbling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Widget < Apotomo::Widget | |
# ... | |
def self.responds_to_event(*options) | |
bubble? = options.last.delete[:bubble] || false | |
unless bubble? | |
# Get the method name | |
method_name = options.last[:with] || options.first | |
# Get the method itself | |
method = self.instance_method(method_name) | |
# Define a new method by calling the old one and stopping the event afterwards | |
define_method(method_name) do |evt| | |
method.bind(self).call(evt) | |
evt.stop! | |
end | |
end | |
super(*options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment