Skip to content

Instantly share code, notes, and snippets.

@Insood
Created June 4, 2018 15:38
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 Insood/0de48607d509f6a527cd1b2c91518fc4 to your computer and use it in GitHub Desktop.
Save Insood/0de48607d509f6a527cd1b2c91518fc4 to your computer and use it in GitHub Desktop.
module IncludableOne
def initialize
super
puts "IncludableOne::initialize"
register_on_save :includable_one_saved
end
def includable_one_saved
puts "Saved One"
end
end
module IncludableTwo
def initialize
super
puts "IncludableTwo::initialize"
register_on_save :includable_two_saved
end
def includable_two_saved
puts "Saved Two"
end
end
class MyBaseClass
def initialize
puts "MyBaseClass::initialize"
@callbacks = []
end
def register_on_save(callback)
@callbacks << callback
end
def save
@callbacks.each do |callback|
send(callback)
end
end
end
class MyDerivedClassOne < MyBaseClass
include IncludableOne
include IncludableTwo
end
class MyDerivedClassTwo < MyBaseClass
include IncludableOne
end
derived1 = MyDerivedClassOne.new()
derived1.save
derived2 = MyDerivedClassTwo.new()
derived2.save
@Insood
Copy link
Author

Insood commented Jun 4, 2018

MyBaseClass::initialize
IncludableOne::initialize
IncludableTwo::initialize
Saved One
Saved Two
MyBaseClass::initialize
IncludableOne::initialize
Saved One

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment