Skip to content

Instantly share code, notes, and snippets.

@bgarret
Created May 20, 2011 15:47
Show Gist options
  • Save bgarret/983207 to your computer and use it in GitHub Desktop.
Save bgarret/983207 to your computer and use it in GitHub Desktop.
Redefine responds_to_event to allow preventing event bubbling
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