Created
October 31, 2010 21:36
-
-
Save tcocca/657184 to your computer and use it in GitHub Desktop.
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
module ThemeMixin | |
=begin | |
# Rails only w/ alias_method_chain | |
def self.included(base) | |
base.alias_method_chain(:base_method, :override) | |
end | |
def base_method_with_override | |
"theme index" | |
end | |
=end | |
# Plain ruby - need alias_method | |
def self.included(base) | |
base.class_eval do | |
alias_method :base_method_original, :base_method | |
alias_method :base_method, :base_method_themed | |
end | |
end | |
def base_method_themed | |
"theme index" | |
end | |
end | |
class App | |
def base_method | |
"index" | |
end | |
def self.remove_mixin(mod) | |
mod.instance_methods.each do |method_name| | |
if method_name =~ /^(.*)_themed$/ | |
self.class_eval do | |
alias_method $1, "#{$1}_original" | |
alias_method "#{$1}_themed", $1 | |
end | |
end | |
end | |
end | |
end | |
puts " -- before mixin -- " | |
puts App.new.base_method | |
App.send(:include, ThemeMixin) | |
puts " -- after mixin -- " | |
puts App.new.base_method | |
puts App.new.base_method_original | |
puts App.new.base_method_themed | |
App.remove_mixin(ThemeMixin) | |
puts " -- after removal -- " | |
puts App.new.base_method | |
puts App.new.base_method_original | |
puts App.new.base_method_themed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment