lib/tapioca/runtime/reflection.rb in tapioca-0.9.2 vs lib/tapioca/runtime/reflection.rb in tapioca-0.9.3

- old
+ new

@@ -156,28 +156,28 @@ end # Examines the call stack to identify the closest location where a "require" is performed # by searching for the label "<top (required)>". If none is found, it returns the location # labeled "<main>", which is the original call site. - sig { params(locations: T.nilable(T::Array[Thread::Backtrace::Location])).returns(String) } - def resolve_loc(locations) + sig { returns(String) } + def required_from_location + locations = Kernel.caller_locations return "" unless locations - resolved_loc = locations.find { |loc| REQUIRED_FROM_LABELS.include?(loc.label) } - return "" unless resolved_loc + required_location = locations.find { |loc| REQUIRED_FROM_LABELS.include?(loc.label) } + return "" unless required_location - resolved_loc.absolute_path || "" + required_location.absolute_path || "" end - sig { params(constant: Module).returns(T.nilable(String)) } - def constant_name_from_singleton_class(constant) - constant.to_s.match("#<Class:(.+)>")&.captures&.first - end + sig { params(singleton_class: Module).returns(T.nilable(Module)) } + def attached_class_of(singleton_class) + # https://stackoverflow.com/a/36622320/98634 + result = ObjectSpace.each_object(singleton_class).find do |klass| + singleton_class_of(T.cast(klass, Module)) == singleton_class + end - sig { params(constant: Module).returns(T.nilable(BasicObject)) } - def constant_from_singleton_class(constant) - constant_name = constant_name_from_singleton_class(constant) - constantize(constant_name) if constant_name + T.cast(result, Module) end end end end