Module: Tapioca::Runtime::Reflection

Constant Summary collapse

CLASS_METHOD =
T.let(Kernel.instance_method(:class), UnboundMethod)
CONSTANTS_METHOD =
T.let(Module.instance_method(:constants), UnboundMethod)
NAME_METHOD =
T.let(Module.instance_method(:name), UnboundMethod)
SINGLETON_CLASS_METHOD =
T.let(Object.instance_method(:singleton_class), UnboundMethod)
ANCESTORS_METHOD =
T.let(Module.instance_method(:ancestors), UnboundMethod)
SUPERCLASS_METHOD =
T.let(Class.instance_method(:superclass), UnboundMethod)
OBJECT_ID_METHOD =
T.let(BasicObject.instance_method(:__id__), UnboundMethod)
EQUAL_METHOD =
T.let(BasicObject.instance_method(:equal?), UnboundMethod)
PUBLIC_INSTANCE_METHODS_METHOD =
T.let(Module.instance_method(:public_instance_methods), UnboundMethod)
PROTECTED_INSTANCE_METHODS_METHOD =
T.let(Module.instance_method(:protected_instance_methods), UnboundMethod)
PRIVATE_INSTANCE_METHODS_METHOD =
T.let(Module.instance_method(:private_instance_methods), UnboundMethod)
METHOD_METHOD =
T.let(Kernel.instance_method(:method), UnboundMethod)

Instance Method Summary collapse

Instance Method Details

#ancestors_of(constant) ⇒ Object



58
59
60
# File 'lib/tapioca/runtime/reflection.rb', line 58

def ancestors_of(constant)
  ANCESTORS_METHOD.bind_call(constant)
end

#are_equal?(object, other) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/tapioca/runtime/reflection.rb', line 73

def are_equal?(object, other)
  EQUAL_METHOD.bind_call(object, other)
end

#class_of(object) ⇒ Object



37
38
39
# File 'lib/tapioca/runtime/reflection.rb', line 37

def class_of(object)
  CLASS_METHOD.bind_call(object)
end

#constantize(symbol, inherit: false, namespace: Object) ⇒ Object



30
31
32
33
34
# File 'lib/tapioca/runtime/reflection.rb', line 30

def constantize(symbol, inherit: false, namespace: Object)
  namespace.const_get(symbol, inherit)
rescue NameError, LoadError, RuntimeError, ArgumentError, TypeError
  nil
end

#constants_of(constant) ⇒ Object



42
43
44
# File 'lib/tapioca/runtime/reflection.rb', line 42

def constants_of(constant)
  CONSTANTS_METHOD.bind_call(constant, false)
end

#descendants_of(klass) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/tapioca/runtime/reflection.rb', line 148

def descendants_of(klass)
  result = ObjectSpace.each_object(klass.singleton_class).reject do |k|
    T.cast(k, Module).singleton_class? || T.unsafe(k) == klass
  end

  T.unsafe(result)
end

#inherited_ancestors_of(constant) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/tapioca/runtime/reflection.rb', line 93

def inherited_ancestors_of(constant)
  if Class === constant
    ancestors_of(superclass_of(constant) || Object)
  else
    Module.ancestors
  end
end

#method_of(constant, method) ⇒ Object



126
127
128
# File 'lib/tapioca/runtime/reflection.rb', line 126

def method_of(constant, method)
  METHOD_METHOD.bind_call(constant, method)
end

#name_of(constant) ⇒ Object



47
48
49
50
# File 'lib/tapioca/runtime/reflection.rb', line 47

def name_of(constant)
  name = NAME_METHOD.bind_call(constant)
  name&.start_with?("#<") ? nil : name
end

#name_of_type(type) ⇒ Object



121
122
123
# File 'lib/tapioca/runtime/reflection.rb', line 121

def name_of_type(type)
  type.to_s.gsub(/\bAttachedClass\b/, "T.attached_class")
end

#object_id_of(object) ⇒ Object



68
69
70
# File 'lib/tapioca/runtime/reflection.rb', line 68

def object_id_of(object)
  OBJECT_ID_METHOD.bind_call(object)
end

#private_instance_methods_of(constant) ⇒ Object



88
89
90
# File 'lib/tapioca/runtime/reflection.rb', line 88

def private_instance_methods_of(constant)
  PRIVATE_INSTANCE_METHODS_METHOD.bind_call(constant)
end

#protected_instance_methods_of(constant) ⇒ Object



83
84
85
# File 'lib/tapioca/runtime/reflection.rb', line 83

def protected_instance_methods_of(constant)
  PROTECTED_INSTANCE_METHODS_METHOD.bind_call(constant)
end

#public_instance_methods_of(constant) ⇒ Object



78
79
80
# File 'lib/tapioca/runtime/reflection.rb', line 78

def public_instance_methods_of(constant)
  PUBLIC_INSTANCE_METHODS_METHOD.bind_call(constant)
end

#qualified_name_of(constant) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/tapioca/runtime/reflection.rb', line 102

def qualified_name_of(constant)
  name = name_of(constant)
  return if name.nil?

  if name.start_with?("::")
    name
  else
    "::#{name}"
  end
end

#signature_of(method) ⇒ Object



114
115
116
117
118
# File 'lib/tapioca/runtime/reflection.rb', line 114

def signature_of(method)
  T::Utils.signature_for_method(method)
rescue LoadError, StandardError
  nil
end

#singleton_class_of(constant) ⇒ Object



53
54
55
# File 'lib/tapioca/runtime/reflection.rb', line 53

def singleton_class_of(constant)
  SINGLETON_CLASS_METHOD.bind_call(constant)
end

#superclass_of(constant) ⇒ Object



63
64
65
# File 'lib/tapioca/runtime/reflection.rb', line 63

def superclass_of(constant)
  SUPERCLASS_METHOD.bind_call(constant)
end