Module: Tapioca::Reflection

Extended by:
T::Sig, Reflection
Included in:
DynamicMixinCompiler, Compilers::Dsl::Base, Compilers::SymbolTable::SymbolGenerator, ConstantLocator, Reflection
Defined in:
lib/tapioca/reflection.rb

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



44
45
46
# File 'lib/tapioca/reflection.rb', line 44

def ancestors_of(constant)
  ANCESTORS_METHOD.bind(constant).call
end

#are_equal?(object, other) ⇒ Boolean

Returns:

  • (Boolean)


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

def are_equal?(object, other)
  EQUAL_METHOD.bind(object).call(other)
end

#class_of(object) ⇒ Object



23
24
25
# File 'lib/tapioca/reflection.rb', line 23

def class_of(object)
  CLASS_METHOD.bind(object).call
end

#constants_of(constant) ⇒ Object



28
29
30
# File 'lib/tapioca/reflection.rb', line 28

def constants_of(constant)
  CONSTANTS_METHOD.bind(constant).call(false)
end

#descendants_of(klass) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/tapioca/reflection.rb', line 130

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



79
80
81
82
83
84
85
# File 'lib/tapioca/reflection.rb', line 79

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

#method_of(constant, method) ⇒ Object



112
113
114
# File 'lib/tapioca/reflection.rb', line 112

def method_of(constant, method)
  METHOD_METHOD.bind(constant).call(method)
end

#name_of(constant) ⇒ Object



33
34
35
36
# File 'lib/tapioca/reflection.rb', line 33

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

#name_of_type(type) ⇒ Object



107
108
109
# File 'lib/tapioca/reflection.rb', line 107

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

#object_id_of(object) ⇒ Object



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

def object_id_of(object)
  OBJECT_ID_METHOD.bind(object).call
end

#private_instance_methods_of(constant) ⇒ Object



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

def private_instance_methods_of(constant)
  PRIVATE_INSTANCE_METHODS_METHOD.bind(constant).call
end

#protected_instance_methods_of(constant) ⇒ Object



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

def protected_instance_methods_of(constant)
  PROTECTED_INSTANCE_METHODS_METHOD.bind(constant).call
end

#public_instance_methods_of(constant) ⇒ Object



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

def public_instance_methods_of(constant)
  PUBLIC_INSTANCE_METHODS_METHOD.bind(constant).call
end

#qualified_name_of(constant) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/tapioca/reflection.rb', line 88

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



100
101
102
103
104
# File 'lib/tapioca/reflection.rb', line 100

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

#singleton_class_of(constant) ⇒ Object



39
40
41
# File 'lib/tapioca/reflection.rb', line 39

def singleton_class_of(constant)
  SINGLETON_CLASS_METHOD.bind(constant).call
end

#superclass_of(constant) ⇒ Object



49
50
51
# File 'lib/tapioca/reflection.rb', line 49

def superclass_of(constant)
  SUPERCLASS_METHOD.bind(constant).call
end