Skip to content

Instantly share code, notes, and snippets.

@noelwarr
Last active December 16, 2015 23:59
Show Gist options
  • Save noelwarr/5517957 to your computer and use it in GitHub Desktop.
Save noelwarr/5517957 to your computer and use it in GitHub Desktop.
Nester nests a module or class into another module.
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