Sha256: b44780b4a4bf946c286a46c7279ed198904649738673193d3629e612cdc05be4

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

cs__scoped_require 'contrast/utils/object_share'

# Some developers override various methods on Module, which can often involve
# changing expected method parity/behavior which in turn prevents us from being
# able to reliably use affected methods. Let's alias these method so that we
# always have access to them.
class Module
  alias_method :cs__name, :name
  alias_method :cs__constants, :constants
  alias_method :cs__const_defined?, :const_defined?
  alias_method :cs__const_get, :const_get
  alias_method :cs__const_set, :const_set
  alias_method :cs__autoload?, :autoload?

  # 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 truly
  # truly defined, meaning it existed before this method was invoked, not as a
  # result of it.
  #
  # @param name [String] the name of the constant to look up
  # @return [Boolean]
  def cs__truly_defined? name
    return false unless name

    segments = name.split(Contrast::Utils::ObjectShare::DOUBLE_COLON)
    previous_module = Module
    segments.each do |segment|
      return false if previous_module.cs__autoload?(segment)
      return false unless previous_module.cs__const_defined?(segment)

      previous_module = previous_module.cs__const_get(segment)
    end

    true
  rescue NameError # account for nonsense / poorly formatted constants
    false
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contrast-agent-3.8.5 lib/contrast/core_extensions/module.rb
contrast-agent-3.8.4 lib/contrast/core_extensions/module.rb