Created
August 19, 2009 17:53
-
-
Save charly/170521 to your computer and use it in GitHub Desktop.
This file contains 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
require "rubygems" | |
require "uninclude" | |
module Core | |
def save; "saved" end | |
end | |
module Dirty | |
def save; "clean + " + super end | |
end | |
module Transaction | |
def save; "transaction + " + super end | |
end | |
module Validation | |
def save; "valid + " + super end | |
end | |
class Base | |
include Core | |
include Dirty | |
include Transaction | |
include Validation | |
end | |
# PLUGIN adding more validation | |
module MyValidation | |
def save | |
"more validation + " + super | |
end | |
end | |
# PLUGIN overiding Transaction | |
module MyTransaction | |
def save | |
Base.send :uninclude, Transaction | |
body = "my transaction! + " + super | |
Base.send :include, Transaction | |
body | |
end | |
end | |
# Our everyday model | |
class A < Base | |
include MyTransaction | |
end | |
class B < Base; end | |
class C < Base | |
include MyValidation | |
end | |
a = A.new | |
b = B.new | |
c = C.new | |
puts a.save #=> my transaction! + valid + clean + saved | |
puts b.save #=> transaction + valid + clean + saved | |
puts c.save #=> more validation + transaction + valid + clean + saved |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment