lib/contrast/utils/class_util.rb in contrast-agent-4.13.1 vs lib/contrast/utils/class_util.rb in contrast-agent-4.14.0

- old
+ new

@@ -52,11 +52,11 @@ # # TODO: RUBY-1327 # Once we move to 2.7+, we can combine the caches using ID b/c the memory location stops being the id # # @param object [Object, nil] the entity to convert to a String - # @return [String] the human readable form of the String, as defined by + # @return [String, Object] the human readable form of the String, as defined by # https://bitbucket.org/contrastsecurity/assess-specifications/src/master/vulnerability/capture-snapshot.md def to_contrast_string object # Only treat object like a string if it actually is a string+ some subclasses of String override string # methods we depend on if object.cs__class == String @@ -64,22 +64,26 @@ @string_cache[object] = to_cached_string(object) || object.dup else return @lru_cache[object.__id__] if @lru_cache.key? object.__id__ - @lru_cache[object.__id__] = if object.nil? - Contrast::Utils::ObjectShare::NIL_STRING - elsif object.cs__is_a?(Symbol) - ":#{ object }" - elsif object.cs__is_a?(Module) || object.cs__is_a?(Class) - "#{ object.cs__name }@#{ object.__id__ }" - elsif object.cs__is_a?(Regexp) - object.source - elsif use_to_s?(object) - object.to_s - else - "#{ object.cs__class.cs__name }@#{ object.__id__ }" - end + @lru_cache[object.__id__] = convert_object object + end + end + + def convert_object object + if object.nil? + Contrast::Utils::ObjectShare::NIL_STRING + elsif object.cs__is_a?(Symbol) + ":#{ object }" + elsif object.cs__is_a?(Module) || object.cs__is_a?(Class) + "#{ object.cs__name }@#{ object.__id__ }" + elsif object.cs__is_a?(Regexp) + object.source + elsif use_to_s?(object) + object.to_s + else + "#{ object.cs__class.cs__name }@#{ object.__id__ }" end end # The method const_defined? can cause autoload, which is bad for us. The method autoload? doesn't traverse # namespaces. This method lets us provide a constant, as a String, and parse it to determine if it has been