Sha256: 36ae90bee01375557a004e32190d5b27b5249d9a3755accea9a8d9d29e07cbab

Contents?: true

Size: 1.08 KB

Versions: 124

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

class ReeObject::DeepFreeze
  include Ree::FnDSL

  fn :deep_freeze

  doc(<<~DOC)
    Deeply freezes object.
      
    Examples of usage:
      deep_freeze(Object.new)
  DOC

  contract(Any => Any)
  def call(obj)
    recursively_freeze(obj, {})
  end

  private

  def recursively_freeze(obj, cache)
    if obj.class == Array
      freeze_array(obj, cache)
    elsif obj.class == Hash
      freeze_hash(obj, cache)
    else
      freeze_obj(obj, cache)
    end
  end

  def freeze_array(array, cache)
    array.each { recursively_freeze(_1, cache) }
    array.freeze
  end

  def freeze_hash(hash, cache)
    hash.default.freeze

    hash.each do |k, v|
      recursively_freeze(k, cache)
      recursively_freeze(v, cache)
    end

    hash.freeze
  end

  def freeze_obj(obj, cache)
    return obj if obj.is_a?(Class) || obj.is_a?(Module)
    return cache[obj.object_id] if cache.key?(obj.object_id)

    cache[obj.object_id] = obj

    obj.instance_variables.each do |var|
      recursively_freeze(obj.instance_variable_get(var), cache)
    end

    obj.freeze
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb