Class: Looksee::Adapter::Base
- Inherits:
-
Object
- Object
- Looksee::Adapter::Base
- Defined in:
- lib/looksee/adapter/base.rb
Instance Method Summary collapse
-
#describe_module(mod) ⇒ Object
Return a description of the given module.
- #has_no_methods?(mod) ⇒ Boolean
- #includes_no_modules?(klass) ⇒ Boolean
- #internal_undefined_instance_methods(mod) ⇒ Object
-
#lookup_modules(object) ⇒ Object
Return the chain of classes and modules which comprise the object’s method lookup path.
- #module_name(mod) ⇒ Object
- #singleton_instance(singleton_class) ⇒ Object
- #undefined_instance_methods(mod) ⇒ Object
Instance Method Details
#describe_module(mod) ⇒ Object
Return a description of the given module.
This is used for the module labels in the Inspector output.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/looksee/adapter/base.rb', line 24 def describe_module(mod) Module === mod or raise TypeError, "expected Module, got: #{mod.inspect}" num_brackets = 0 object = mod while (instance = singleton_instance(object)) num_brackets += 1 object = instance end if object.is_a?(Module) description = module_name(object) if description.empty? is_class = Class === object description = "unnamed #{is_class ? 'Class' : 'Module'}" end else description = "#{module_name(object.class)} instance" end if num_brackets == 0 description else "#{'['*num_brackets}#{description}#{']'*num_brackets}" end end |
#has_no_methods?(mod) ⇒ Boolean
63 64 65 66 67 |
# File 'lib/looksee/adapter/base.rb', line 63 def has_no_methods?(mod) [:public, :protected, :private].all? do |visibility| Looksee.safe_call(Module, "#{visibility}_instance_methods", mod, false).empty? end && undefined_instance_methods(mod).empty? end |
#includes_no_modules?(klass) ⇒ Boolean
69 70 71 72 73 |
# File 'lib/looksee/adapter/base.rb', line 69 def includes_no_modules?(klass) ancestors = Looksee.safe_call(Module, :ancestors, klass) ancestors.size == 1 || klass.included_modules == klass.ancestors[1].included_modules end |
#internal_undefined_instance_methods(mod) ⇒ Object
59 60 61 |
# File 'lib/looksee/adapter/base.rb', line 59 def internal_undefined_instance_methods(mod) raise NotImplementedError, "abstract" end |
#lookup_modules(object) ⇒ Object
Return the chain of classes and modules which comprise the object’s method lookup path.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/looksee/adapter/base.rb', line 8 def lookup_modules(object) start = begin singleton_class = (class << object; self; end) singleton_class unless has_no_methods?(singleton_class) && includes_no_modules?(singleton_class) && !(Class === object) rescue TypeError # immediate object end start ||= Looksee.safe_call(Object, :class, object) Looksee.safe_call(Module, :ancestors, start) end |
#module_name(mod) ⇒ Object
79 80 81 |
# File 'lib/looksee/adapter/base.rb', line 79 def module_name(mod) Looksee.safe_call(Module, :name, mod) || '' end |
#singleton_instance(singleton_class) ⇒ Object
75 76 77 |
# File 'lib/looksee/adapter/base.rb', line 75 def singleton_instance(singleton_class) raise NotImplementedError, "abstract" end |
#undefined_instance_methods(mod) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/looksee/adapter/base.rb', line 51 def undefined_instance_methods(mod) if Module.method_defined?(:undefined_instance_methods) mod.undefined_instance_methods else internal_undefined_instance_methods(mod) end end |