Last active
December 16, 2015 23:59
-
-
Save noelwarr/5517957 to your computer and use it in GitHub Desktop.
Nester nests a module or class into another module.
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
class Nester | |
def initialize | |
@checklist = [] | |
end | |
def nest(source, target, source_sym = nil) | |
if source.is_a?(Module) | |
source_sym = source.name.split("::").last.to_sym if source_sym.nil? | |
if source.is_a?(Class) | |
kls = source.clone | |
else | |
mod = Module.new | |
mod.send(:include, source) | |
end | |
t = kls || mod | |
target.send(:const_set, source_sym, t) | |
source.constants.each{|sym| | |
s = source.send(:const_get, sym) | |
unless @checklist.include?(t) | |
nest(s, t, sym) | |
@checklist.push(t) | |
end | |
} | |
else | |
target.send(:const_set, source_sym, kls) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment