Module: SchemaMonkey::Module
Instance Method Summary collapse
-
#const_lookup(mod, name) ⇒ Object
ruby 2.* supports mod.const_get(“Component::Path”) but ruby 1.9.3 doesn’t.
- #descendants(mod, can_load: nil) ⇒ Object
- #insert(base, mod) ⇒ Object
- #mkpath(mod, path) ⇒ Object
Instance Method Details
#const_lookup(mod, name) ⇒ Object
ruby 2.* supports mod.const_get(“Component::Path”) but ruby 1.9.3 doesn’t. And neither has an option to return nil rather than raising a NameError
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/schema_monkey/module.rb', line 16 def const_lookup(mod, name) name.to_s.split('::').map(&:to_sym).each do |component| begin mod = mod.const_get(component, false) rescue NameError return nil end end mod end |
#descendants(mod, can_load: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/schema_monkey/module.rb', line 27 def descendants(mod, can_load: nil) consts, auto = mod.constants.group_by{|c| !!mod.autoload?(c)}.values_at(false, true) consts ||= [] consts += auto.select { |it| it.to_s =~ can_load } if can_load and auto children = consts.map{|c| mod.const_get(c) }.select { |obj| obj.is_a?(::Module) rescue nil } children + children.flat_map {|c| descendants(c, can_load: can_load) } end |
#insert(base, mod) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/schema_monkey/module.rb', line 5 def insert(base, mod) if mod.respond_to? :included base.send(:include, mod) unless base.include? mod else base.send(:prepend, mod) end end |
#mkpath(mod, path) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/schema_monkey/module.rb', line 37 def mkpath(mod, path) path.split('::').each do |component| mod = const_lookup(mod, component) || mod.const_set(component, ::Module.new) end mod end |