Sha256: 65acd060a863603b928351077f3247f0c2aae61d59b2195c7e87b0b4e91ccb83

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 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/core_extensions/object'
cs__scoped_require 'contrast/utils/duck_utils'

module Contrast
  module Utils
    # This utility allows us to act on frozen objects, creating an unfrozen
    # duplicate in those cases where that is possible.
    class FreezeUtil
      class << self
        # Make every attempt to duplicate the frozen object so that it can
        # be tracked. Some things, like Nil, True, and False, respond to dup
        # but throw a TypeError. This catches that case and returns the
        # original. We luck out as these three cases do not get propagated to
        #
        # @param original [Object] something frozen, usually a String
        # @return [Object] the original or an unfrozen copy
        def unfreeze_dup original
          return original unless original.cs__frozen?
          return original unless Contrast::Utils::DuckUtils.quacks_like_duplicable?(original)

          copy = original.dup
          if Contrast::Utils::DuckUtils.quacks_like_tracked_hash?(copy)
            copy.each_key do |key|
              value = original[key]
              copy[key] = Contrast::Utils::DuckUtils.quacks_like_duplicable?(original) ? value.dup : value
            end
          end
          copy
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contrast-agent-3.8.5 lib/contrast/utils/freeze_util.rb
contrast-agent-3.8.4 lib/contrast/utils/freeze_util.rb