Module: Tapioca::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



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

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

#are_equal?(object, other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#class_of(object) ⇒ Object



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

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

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



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

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

#constants_of(constant) ⇒ Object



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

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

#descendants_of(klass) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/tapioca/reflection.rb', line 143

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



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

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

#method_of(constant, method) ⇒ Object



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

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

#name_of(constant) ⇒ Object



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

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

#name_of_type(type) ⇒ Object



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

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

#object_id_of(object) ⇒ Object



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

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

#private_instance_methods_of(constant) ⇒ Object



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

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

#protected_instance_methods_of(constant) ⇒ Object



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

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

#public_instance_methods_of(constant) ⇒ Object



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

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

#qualified_name_of(constant) ⇒ Object



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

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



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

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

#singleton_class_of(constant) ⇒ Object



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

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

#superclass_of(constant) ⇒ Object



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

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