Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created October 31, 2010 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcocca/657184 to your computer and use it in GitHub Desktop.
Save tcocca/657184 to your computer and use it in GitHub Desktop.
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