module Aquarium::Utils::TypeUtils
Public Class Methods
descendents(clazz)
click to toggle source
# File lib/aquarium/utils/type_utils.rb 11 def self.descendents clazz 12 visited = Set.new([Class, Object, Module, clazz]) 13 if RUBY_VERSION =~ /^1.9/ 14 visited << BasicObject 15 end 16 result = [clazz] 17 Module.constants.each do |const| 18 mod = Module.class_eval(const.to_s) 19 if mod.respond_to?(:ancestors) 20 result << mod if mod.ancestors.include?(clazz) 21 do_descendents clazz, mod, visited, result 22 end 23 end 24 result.uniq 25 end
is_type?(type_or_object)
click to toggle source
# File lib/aquarium/utils/type_utils.rb 7 def self.is_type? type_or_object 8 type_or_object.kind_of?(Class) or type_or_object.kind_of?(Module) 9 end
nested(clazz)
click to toggle source
# File lib/aquarium/utils/type_utils.rb 27 def self.nested clazz 28 result = [clazz] 29 clazz.constants.each do |const| 30 mod = clazz.class_eval(const.to_s) 31 next unless is_type?(mod) 32 result << mod 33 result << nested(mod) 34 end 35 result.flatten.uniq 36 end
Protected Class Methods
do_descendents(clazz, visiting_module, visited, result)
click to toggle source
For JRuby classes, we have to “__x__” forms of the reflection methods that don't end in '?'. That includes “send”, so we do some ugly switching, rather than call “mod.send(method_name)” or “mod.__send__(method_name)”! TODO: is this still true with the latest JRUBY versions?
# File lib/aquarium/utils/type_utils.rb 44 def self.do_descendents clazz, visiting_module, visited, result 45 visited << visiting_module 46 use_underscore_methods = use_underscore_methods? visiting_module 47 nested_constants = use_underscore_methods ? visiting_module.__constants__ : visiting_module.constants 48 nested_constants.each do |const| 49 next unless visiting_module.const_defined?(const) 50 nested_module = ignore_warning do 51 use_underscore_methods ? visiting_module.__const_get__(const) : visiting_module.const_get(const) 52 end 53 next if visited.include?(nested_module) 54 next unless responds_to_ancestors?(nested_module) 55 use_underscore_methods2 = use_underscore_methods? nested_module 56 modules_ancestors = use_underscore_methods2 ? nested_module.__ancestors__ : nested_module.ancestors 57 result << nested_module if modules_ancestors.include?(clazz) 58 do_descendents clazz, nested_module, visited, result 59 end 60 end
ignore_warning() { || ... }
click to toggle source
# File lib/aquarium/utils/type_utils.rb 72 def self.ignore_warning 73 warning_orig = $-w 74 $-w = nil 75 res = yield 76 $-w = warning_orig 77 return res 78 end
responds_to_ancestors?(mod)
click to toggle source
# File lib/aquarium/utils/type_utils.rb 68 def self.responds_to_ancestors? mod 69 mod.respond_to?(:ancestors) or mod.respond_to?(:__ancestors__) 70 end
use_underscore_methods?(mod)
click to toggle source
# File lib/aquarium/utils/type_utils.rb 62 def self.use_underscore_methods? mod 63 #TODO 64 false 65 # mod.respond_to?(:__constants__) 66 end