Created
October 24, 2013 11:03
-
-
Save bravoecho/7135185 to your computer and use it in GitHub Desktop.
Dynamically build and include a parameterized module. Similar to the Sequel gem inheritance mechanism.
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 Awesome | |
def self.Stuff(thingy) | |
Module.new do | |
class_methods = Module.new do | |
attr_accessor :thingy | |
end | |
define_singleton_method :included do |klass| | |
klass.extend(class_methods) | |
klass.thingy = thingy | |
end | |
def awesome_stuff | |
"I'm awesome indeed" | |
end | |
end | |
end | |
end | |
class NotSoAwesome | |
include Awesome::Stuff(:piece_of_cake) | |
end | |
NotSoAwesome.thingy # => :piece_of_cake | |
my_obj = NotSoAwesome.new | |
my_obj.awesome_stuff # => "I'm awesome indeed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment